├── .editorconfig ├── .gitignore ├── .idea ├── libraries │ └── Dart_SDK.xml ├── modules.xml └── workspace.xml ├── .metadata ├── .vscode └── launch.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── android ├── app │ └── src │ │ └── main │ │ └── java │ │ └── io │ │ └── flutter │ │ └── plugins │ │ └── GeneratedPluginRegistrant.java └── local.properties ├── flutter_daydart.iml ├── lib ├── flutter_daydart.dart └── src │ ├── DayDart.dart │ ├── Units.dart │ └── utils.dart ├── pubspec.lock ├── pubspec.yaml └── test └── flutter_daydart_test.dart /.editorconfig: -------------------------------------------------------------------------------- 1 | # top-most EditorConfig file 2 | root = true 3 | 4 | # Unix-style newlines with a newline ending every file 5 | [*] 6 | end_of_line = lf 7 | insert_final_newline = true 8 | 9 | # Matches multiple files with brace expansion notation 10 | # Set default charset 11 | [*.dart] 12 | charset = utf-8 13 | indent_style = space 14 | indent_size = 2 15 | trim_trailing_whitespace = true 16 | 17 | # Matches the exact files either package.json or .travis.yml 18 | [{pubspec.yaml}] 19 | indent_style = space 20 | indent_size = 2 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .dart_tool/ 3 | 4 | .packages 5 | .pub/ 6 | 7 | build/ 8 | ios/.generated/ 9 | ios/Flutter/Generated.xcconfig 10 | ios/Runner/GeneratedPluginRegistrant.* 11 | -------------------------------------------------------------------------------- /.idea/libraries/Dart_SDK.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/workspace.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /.metadata: -------------------------------------------------------------------------------- 1 | # This file tracks properties of this Flutter project. 2 | # Used by Flutter tool to assess capabilities and perform upgrades etc. 3 | # 4 | # This file should be version controlled and should not be manually edited. 5 | 6 | version: 7 | revision: 5391447fae6209bb21a89e6a5a6583cac1af9b4b 8 | channel: stable 9 | 10 | project_type: package 11 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "name": "Flutter", 9 | "request": "launch", 10 | "type": "dart" 11 | } 12 | ] 13 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [0.0.1] - TODO: Add release date. 2 | 3 | * TODO: Describe initial release. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 子曰五溪 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DayDart 2 | 3 | A new Flutter package project. 4 | 5 | ![https://img.shields.io/github/license/icepy/flutter_daydart.svg](https://img.shields.io/github/license/icepy/flutter_daydart.svg) 6 | ![https://img.shields.io/github/issues/icepy/flutter_daydart.svg](https://img.shields.io/github/issues/icepy/flutter_daydart.svg) 7 | 8 | ## Getting Started 9 | 10 | This project is a starting point for a Dart 11 | [package](https://flutter.io/developing-packages/), 12 | a library module containing code that can be shared easily across 13 | multiple Flutter or Dart projects. 14 | 15 | For help getting started with Flutter, view our 16 | [online documentation](https://flutter.io/docs), which offers tutorials, 17 | samples, guidance on mobile development, and a full API reference. 18 | 19 | ## API 20 | 21 | `DayDart` 对象是不可变的,所有的调用都将返回一个新的 `DayDart` 对象。 22 | 23 | - [解析](#解析) 24 | - [当前时间](#当前时间) 25 | - [原生创建](#原生创建) 26 | - [时间字符串](#时间字符串) 27 | - [DateTime 对象](#DateTime-对象) 28 | - [复制](#复制) 29 | - [验证](#验证) 30 | - [获取](#获取) 31 | - [年](#年) 32 | - [月](#月) 33 | - [日](#日) 34 | - [星期](#星期) 35 | - [时](#时) 36 | - [分](#分) 37 | - [秒](#秒) 38 | - [毫秒](#毫秒) 39 | - [操作](#操作) 40 | - [增加](#增加) 41 | - [减少](#减少) 42 | - [查询](#查询) 43 | - [是否相等](#是否相等) 44 | - [是否之前](#是否之前) 45 | - [是否之后](#是否之后) 46 | - [判断是否为 DayDart](判断是否为-DayDart) 47 | - [显示](#显示) 48 | - [格式化](#格式化) 49 | - [DateTime 对象](#DateTime-对象) 50 | - [ISO 8601 字符串](#ISO-8601-字符串) 51 | - [List](#List) 52 | - [Map](#Map) 53 | - [字符串](#字符串) 54 | 55 | ---- 56 | 57 | ### 解析 58 | 59 | 在 `DayDart` 中传入支持的格式。 60 | 61 | #### 当前时间 62 | 63 | 直接运行 `DayDart` 获取当前时间和日期的 `DayDart` 对象。 64 | 65 | ```dart 66 | DayDart() 67 | ``` 68 | 69 | #### 原生创建 70 | 71 | 可以像 DateTime 一样的创建 DayDart 对象 72 | 73 | ```dart 74 | DayDart(2019,12,12); 75 | ``` 76 | 77 | #### 时间字符串 78 | 79 | 传入的一个标准的ISO 8601时间字符串。 80 | 81 | ```dart 82 | DayDart.fromString('2019-02-21'); 83 | ``` 84 | 85 | #### DateTime 对象 86 | 87 | 传入的一个 DateTime 对象。 88 | 89 | ```dart 90 | DayDart.fromDateTime(new DateTime.now()); 91 | ``` 92 | 93 | #### 复制 94 | 95 | `DayDart` 对象是不可变的,因此可以执行 `.clone()` 来获取一个 DayDart 对象的拷贝。 96 | 97 | ```dart 98 | DayDart().clone(); 99 | ``` 100 | 101 | #### 验证 102 | 103 | - return bool 104 | 105 | 检测当前的 DayDart 对象是否是一个有效的时间。 106 | 107 | ```dart 108 | DayDart().isValid(); 109 | ``` 110 | 111 | ### 获取 112 | 113 | 获取日期 114 | 115 | #### 年 116 | 117 | - return int 118 | 119 | 获取年份。 120 | 121 | ```dart 122 | DayDart().year(); 123 | ``` 124 | 125 | #### 月 126 | 127 | - return int 128 | 129 | 获取月份 130 | 131 | ```dart 132 | DayDart().month(); 133 | ``` 134 | 135 | #### 日 136 | 137 | - return int 138 | 139 | 获取日期。 140 | 141 | ```dart 142 | DayDart().date(); 143 | ``` 144 | 145 | #### 星期 146 | 147 | - return int 148 | 149 | 获取星期。 150 | 151 | ```dart 152 | DayDart().day(); 153 | ``` 154 | 155 | #### 时 156 | 157 | - return int 158 | 159 | 获取小时。 160 | 161 | ```dart 162 | DayDart().hour(); 163 | ``` 164 | 165 | #### 分 166 | 167 | - return int 168 | 169 | 获取分钟。 170 | 171 | ```dart 172 | DayDart().minute(); 173 | ``` 174 | 175 | #### 秒 176 | 177 | - return int 178 | 179 | 获取秒。 180 | 181 | ```dart 182 | DayDart().second(); 183 | ``` 184 | 185 | #### 毫秒 186 | 187 | - return int 188 | 189 | 获取毫秒。 190 | 191 | ```dart 192 | DayDart().millisecond(); 193 | ``` 194 | 195 | ### 操作 196 | 197 | 我们可以对 `DayDart` 对象增加如下的操作: 198 | 199 | ```dart 200 | DayDart().add(2, Units.H).subtract(13, Units.M); 201 | ``` 202 | 203 | #### 增加 204 | 205 | 为时间日期进行增加并返回一个新的 `DayDart` 对象。 206 | 207 | ```dart 208 | DayDart().add(2, Units.H); 209 | ``` 210 | 211 | #### 减少 212 | 213 | 为时间日期进行减少并返回一个新的 `DayDart` 对象。 214 | 215 | ```dart 216 | DayDart().subtract(13, Units.M) 217 | ``` 218 | 219 | ### 查询 220 | 221 | Value 系列比对详情如下: 222 | 223 | | enum | Output | 224 | | ------ | ---------------- | 225 | | Units.Y | bool | 226 | | Units.M | bool | 227 | | Units.D | bool | 228 | | Units.H | bool | 229 | | Units.MIN | bool | 230 | 231 | #### 是否相等 232 | 233 | - return bool 234 | 235 | 检查另外一个 DayDart 对象是否与当前 DayDart 对象的时间相等。 236 | 237 | ```dart 238 | DayDart().isSame(daydart); 239 | ``` 240 | 241 | - return bool 242 | 243 | ```dart 244 | DayDart().isSameValue(2019, Units.Y); 245 | ``` 246 | 247 | #### 是否之前 248 | 249 | - return bool 250 | 251 | 检查另外一个 DayDart 对象是否在当前 DayDart 对象时间之前。 252 | 253 | ```dart 254 | DayDart().isBefore(daydart); 255 | ``` 256 | 257 | - return bool 258 | 259 | ```dart 260 | DayDart().isBeforeValue(2019, Units.Y); 261 | ``` 262 | 263 | #### 是否之后 264 | 265 | - return bool 266 | 267 | 检查另外一个 DayDart 对象是否在当前 DayDart 对象时间之后。 268 | 269 | ```dart 270 | DayDart().isAfter(daydart); 271 | ``` 272 | 273 | - return bool 274 | 275 | ```dart 276 | DayDart().isAfterValue(2019, Units.Y); 277 | ``` 278 | 279 | #### 判断是否为 DayDart 280 | 281 | ```dart 282 | DayDart.isDayDart(daydart) 283 | ``` 284 | 285 | ### 显示 286 | 287 | 格式化 DayDart 对象并展示。 288 | 289 | #### 格式化 290 | 291 | - return String 292 | 293 | ```dart 294 | DayDart().format(fm: 'YYYY-MM-DD HH:mm:ss'); 295 | ``` 296 | 297 | 详情如下: 298 | 299 | | Format | Output | Description | 300 | | ------ | ---------------- | ---------------------------- | 301 | | `YY` | 19 | 两位数的年份 | 302 | | `YYYY` | 2019 | 四位数的年份 | 303 | | `M` | 1-12 | 月份,从 1 开始 | 304 | | `MM` | 01-12 | 月份,两位数 | 305 | | `D` | 1-31 | 月份里的一天 | 306 | | `DD` | 01-31 | 月份里的一天,两位数 | 307 | | `H` | 0-23 | 小时 | 308 | | `HH` | 00-23 | 小时,两位数 | 309 | | `m` | 0-59 | 分钟 | 310 | | `mm` | 00-59 | 分钟,两位数 | 311 | | `s` | 0-59 | 秒 | 312 | | `ss` | 00-59 | 秒,两位数 | 313 | | `a` | am pm | | 314 | | `A` | AM PM | | 315 | 316 | 317 | #### DateTime 对象 318 | 319 | - return DateTime 320 | 321 | 返回原生 DateTime 对象 322 | 323 | ```dart 324 | DayDart().toDateTime(); 325 | ``` 326 | 327 | #### ISO 8601 字符串 328 | 329 | - return String 330 | 331 | 返回 ISO8601 格式的字符串。 332 | 333 | ```dart 334 | DayDart().toISOString(); 335 | ``` 336 | 337 | #### List 338 | 339 | - return List 340 | 341 | 返回包含时间数值的 List。 342 | 343 | ```dart 344 | DayDart().toList(); 345 | ``` 346 | 347 | #### Map 348 | 349 | - return Map 350 | 351 | 返回包含时间数值的 Map。 352 | 353 | ```dart 354 | DayDart().toMap(); 355 | ``` 356 | 357 | #### 字符串 358 | 359 | - return String 360 | 361 | ```dart 362 | DayDart().toString(); 363 | ``` 364 | -------------------------------------------------------------------------------- /android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java: -------------------------------------------------------------------------------- 1 | package io.flutter.plugins; 2 | 3 | import io.flutter.plugin.common.PluginRegistry; 4 | 5 | /** 6 | * Generated file. Do not edit. 7 | */ 8 | public final class GeneratedPluginRegistrant { 9 | public static void registerWith(PluginRegistry registry) { 10 | if (alreadyRegisteredWith(registry)) { 11 | return; 12 | } 13 | } 14 | 15 | private static boolean alreadyRegisteredWith(PluginRegistry registry) { 16 | final String key = GeneratedPluginRegistrant.class.getCanonicalName(); 17 | if (registry.hasPlugin(key)) { 18 | return true; 19 | } 20 | registry.registrarFor(key); 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /android/local.properties: -------------------------------------------------------------------------------- 1 | sdk.dir=/Users/xiangwenwen/Library/Android/sdk 2 | flutter.sdk=/Users/xiangwenwen/flutter 3 | flutter.versionName=0.0.3 -------------------------------------------------------------------------------- /flutter_daydart.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /lib/flutter_daydart.dart: -------------------------------------------------------------------------------- 1 | library daydart; 2 | 3 | export 'src/DayDart.dart'; 4 | export 'src/Units.dart'; 5 | -------------------------------------------------------------------------------- /lib/src/DayDart.dart: -------------------------------------------------------------------------------- 1 | import 'dart:core'; 2 | import 'package:flutter_daydart/src/utils.dart'; 3 | import 'package:flutter_daydart/src/Units.dart'; 4 | 5 | 6 | const String FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ss'; 7 | 8 | DayDart wrapper(DateTime date) { 9 | return DayDart.fromDateTime(date); 10 | } 11 | 12 | class DayDart { 13 | 14 | DateTime _date; 15 | 16 | DayDart(){ 17 | this._date = new DateTime.now(); 18 | } 19 | 20 | DayDart.fromDateTime(DateTime date){ 21 | this._date = date; 22 | } 23 | 24 | DayDart.fromString(String iso8601){ 25 | this._date = DateTime.parse(iso8601); 26 | } 27 | 28 | DayDart.fromInt(int year, 29 | [int month = 1, 30 | int day = 1, 31 | int hour = 0, 32 | int minute = 0, 33 | int second = 0, 34 | int millisecond = 0, 35 | int microsecond = 0]){ 36 | this._date = DateTime(year, month, day, hour, minute, second, millisecond, microsecond); 37 | } 38 | 39 | static bool isDayDart(daydart){ 40 | return daydart is DayDart; 41 | } 42 | 43 | /** 44 | * 检测当前 DayDart 对象是一个有效的 DateTime 45 | */ 46 | bool isValid(){ 47 | return _date is DateTime; 48 | } 49 | 50 | /** 51 | * 获取年 52 | */ 53 | int year(){ 54 | return _date.year; 55 | } 56 | 57 | /** 58 | * 获取月 59 | */ 60 | int month(){ 61 | return _date.month; 62 | } 63 | 64 | /** 65 | * 获取日 66 | */ 67 | int date(){ 68 | return _date.day; 69 | } 70 | 71 | /** 72 | * 获取星期 73 | */ 74 | int day(){ 75 | return _date.weekday; 76 | } 77 | 78 | /** 79 | * 获取小时 80 | */ 81 | int hour(){ 82 | return _date.hour; 83 | } 84 | 85 | /** 86 | * 获取分钟 87 | */ 88 | int minute(){ 89 | return _date.minute; 90 | } 91 | 92 | /** 93 | * 获取秒 94 | */ 95 | int second(){ 96 | return _date.second; 97 | } 98 | 99 | /** 100 | * 获取毫秒 101 | */ 102 | int millisecond(){ 103 | return _date.millisecond; 104 | } 105 | 106 | /** 107 | * 返回包含时间数值的 List 108 | */ 109 | List toList(){ 110 | return [ 111 | _date.year, 112 | _date.month, 113 | _date.day, 114 | _date.hour, 115 | _date.minute, 116 | _date.second, 117 | _date.millisecond, 118 | ]; 119 | } 120 | 121 | /** 122 | * 返回 ISO8601 格式的字符串 123 | */ 124 | String toISOString(){ 125 | return _date.toIso8601String(); 126 | } 127 | 128 | /** 129 | * 返回时间对象的字符串 130 | */ 131 | String toString(){ 132 | return _date.toString(); 133 | } 134 | 135 | /** 136 | * 返回当前的 DateTime 对象 137 | */ 138 | DateTime toDateTime(){ 139 | return _date; 140 | } 141 | 142 | /** 143 | * 返回包含时间数值的 Map 144 | */ 145 | Map toMap(){ 146 | return { 147 | 'years':_date.year, 148 | 'months':_date.month, 149 | 'date':_date.day, 150 | 'hours':_date.hour, 151 | 'minutes':_date.minute, 152 | 'seconds':_date.second, 153 | 'milliseconds':_date.millisecond, 154 | }; 155 | } 156 | 157 | /** 158 | * 复制 DayDart 对象 159 | */ 160 | DayDart clone([DateTime dateTime]){ 161 | if (dateTime !=null) { 162 | return wrapper(dateTime); 163 | } else { 164 | return wrapper(toDateTime()); 165 | } 166 | } 167 | 168 | /** 169 | * 接收一系列的时间日期字符串并替换成相应的值 170 | */ 171 | String format({ String fm = FORMAT_DEFAULT }){ 172 | final y = year().toString(); 173 | final mh = month().toString(); 174 | final d = date().toString(); 175 | final h = hour().toString(); 176 | final m = minute().toString(); 177 | final s = second().toString(); 178 | 179 | final matches = { 180 | 'YYYY':y, 181 | 'YY':y.substring(y.length - 2, y.length), 182 | 'M':mh, 183 | 'MM':addZero(mh), 184 | 'D':d, 185 | 'DD':addZero(d), 186 | 'H':h, 187 | 'HH':addZero(h), 188 | 'm':m, 189 | 'mm':addZero(m), 190 | 's':s, 191 | 'ss':addZero(s), 192 | 'a':hour() < 12 ? 'am' : 'pm', 193 | 'A':hour() < 12 ? 'AM' : 'PM', 194 | }; 195 | return fm.replaceAllMapped(new RegExp(r'\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS'), (Match m){ 196 | final match = m.group(0); 197 | if (match.indexOf('[') > -1){ 198 | return match.replaceAll(new RegExp(r'\[|\]'), ''); 199 | } 200 | return matches[match]; 201 | }); 202 | } 203 | 204 | /** 205 | * 检查另外一个 DayDart 对象是否与当前 DayDart 对象的时间相等 206 | */ 207 | bool isSame(DayDart that){ 208 | return _date.isAtSameMomentAs(that.toDateTime()); 209 | } 210 | 211 | /** 212 | * 检查另外一个时间数值 是否与当前 DayDart 相应枚举相等 213 | */ 214 | bool isSameValue(int that, Units unit){ 215 | switch (unit) { 216 | case Units.Y: 217 | return that == year(); 218 | break; 219 | case Units.M: 220 | return that == month(); 221 | break; 222 | case Units.D: 223 | return that == date(); 224 | break; 225 | case Units.H: 226 | return that == hour(); 227 | break; 228 | case Units.MIN: 229 | return that == minute(); 230 | break; 231 | default: 232 | } 233 | } 234 | 235 | /** 236 | * 检查另外一个 DayDart 对象是否在当前 DayDart 对象时间之后 237 | */ 238 | bool isAfter(DayDart that){ 239 | return _date.isAfter(that.toDateTime()); 240 | } 241 | 242 | /** 243 | * 检查另外一个时间数值 是否在当前 DayDart 相应枚举之后 244 | */ 245 | bool isAfterValue(int that, Units unit){ 246 | switch (unit) { 247 | case Units.Y: 248 | return that < year(); 249 | break; 250 | case Units.M: 251 | return that < month(); 252 | break; 253 | case Units.D: 254 | return that < date(); 255 | break; 256 | case Units.H: 257 | return that < hour(); 258 | break; 259 | case Units.MIN: 260 | return that < minute(); 261 | break; 262 | default: 263 | 264 | } 265 | } 266 | 267 | /** 268 | * 检查另外一个 DayDart 对象是否在当前 DayDart 对象时间之前 269 | */ 270 | bool isBefore(DayDart that){ 271 | return _date.isBefore(that.toDateTime()); 272 | } 273 | 274 | /** 275 | * 检查另外一个时间数值 是否在当前 DayDart 相应枚举之前 276 | */ 277 | bool isBeforeValue(int that, Units unit){ 278 | switch (unit) { 279 | case Units.Y: 280 | return that > year(); 281 | break; 282 | case Units.M: 283 | return that > month(); 284 | break; 285 | case Units.D: 286 | return that > date(); 287 | break; 288 | case Units.H: 289 | return that > hour(); 290 | break; 291 | case Units.MIN: 292 | return that > minute(); 293 | break; 294 | default: 295 | } 296 | } 297 | 298 | /** 299 | * 操作时间-增加 300 | */ 301 | DayDart add(int num, Units unit){ 302 | int y = year(); 303 | int m = month(); 304 | int d = date(); 305 | int h = hour(); 306 | int min = minute(); 307 | int s = second(); 308 | int ms = millisecond(); 309 | switch (unit) { 310 | case Units.Y: 311 | return clone(new DateTime( 312 | y + num, 313 | m, 314 | d, 315 | h, 316 | min, 317 | s, 318 | ms 319 | )); 320 | break; 321 | case Units.M: 322 | int afterMonth = m + num; 323 | if (afterMonth <= 12) { 324 | // 小于或等于 12 还是当年 325 | return clone(new DateTime(y, afterMonth, d, h, min, s, ms)); 326 | } else { 327 | // 大于12 就是第二年了 328 | int ad = 12 - m; 329 | int bv = num - ad; 330 | y = y + 1; 331 | if ((bv/12) < 1) { 332 | return clone(new DateTime(y, bv, d, h, min, s, ms)); 333 | } else { 334 | double dY = bv / 12; 335 | int newY = dY.floor(); 336 | int absMonth = newY * 12; 337 | int newMonth = bv - absMonth; 338 | y = y + newY; 339 | return clone(new DateTime(y, newMonth, d, h, min, s, ms)); 340 | } 341 | } 342 | break; 343 | case Units.D: 344 | return clone(_date.add(new Duration(days: num))); 345 | break; 346 | case Units.H: 347 | return clone(_date.add(new Duration(hours:num))); 348 | break; 349 | case Units.MIN: 350 | return clone(_date.add(new Duration(minutes:num))); 351 | break; 352 | case Units.S: 353 | return clone(_date.add(new Duration(seconds: num))); 354 | break; 355 | case Units.MS: 356 | return clone(_date.add(new Duration(milliseconds:num))); 357 | break; 358 | default: 359 | } 360 | } 361 | 362 | /** 363 | * 操作时间-减少 364 | */ 365 | DayDart subtract(int num, Units unit){ 366 | int y = year(); 367 | int m = month(); 368 | int d = date(); 369 | int h = hour(); 370 | int min = minute(); 371 | int s = second(); 372 | int ms = millisecond(); 373 | switch (unit) { 374 | case Units.Y: 375 | return clone(new DateTime( 376 | y - num, 377 | m, 378 | d, 379 | h, 380 | min, 381 | s, 382 | ms 383 | )); 384 | break; 385 | case Units.M: 386 | double dY = num / 12; 387 | if (dY < 1){ 388 | // 说明减的月数没有超过一年 389 | int rangeM = m - num; 390 | if (rangeM < 0) { 391 | y = y - 1; 392 | m = 12 + rangeM; 393 | return clone(new DateTime( 394 | y, 395 | m, 396 | d, 397 | h, 398 | min, 399 | s, 400 | ms 401 | )); 402 | } else { 403 | return clone(new DateTime( 404 | y, 405 | rangeM, 406 | d, 407 | h, 408 | min, 409 | s, 410 | ms 411 | )); 412 | } 413 | } else { 414 | // 减的月数超过了一年 415 | int cY = dY.ceil(); 416 | int bY = dY.floor(); 417 | int cMonth = num - (12 * bY); 418 | int rangeM = m - cMonth; 419 | if (rangeM < 0) { 420 | m = 12 + rangeM; 421 | return clone(new DateTime( 422 | y - cY, 423 | m, 424 | d, 425 | h, 426 | min, 427 | s, 428 | ms 429 | )); 430 | } else { 431 | return clone(new DateTime( 432 | y - bY, 433 | rangeM, 434 | d, 435 | h, 436 | min, 437 | s, 438 | ms 439 | )); 440 | } 441 | } 442 | break; 443 | case Units.D: 444 | return clone(_date.subtract(new Duration(days: num))); 445 | break; 446 | case Units.H: 447 | return clone(_date.subtract(new Duration(hours:num))); 448 | break; 449 | case Units.MIN: 450 | return clone(_date.subtract(new Duration(minutes:num))); 451 | break; 452 | case Units.S: 453 | return clone(_date.subtract(new Duration(seconds: num))); 454 | break; 455 | case Units.MS: 456 | return clone(_date.subtract(new Duration(milliseconds:num))); 457 | break; 458 | default: 459 | } 460 | } 461 | } 462 | -------------------------------------------------------------------------------- /lib/src/Units.dart: -------------------------------------------------------------------------------- 1 | enum Units { 2 | Y, 3 | M, 4 | W, 5 | D, 6 | H, 7 | MIN, 8 | S, 9 | MS, 10 | DATE, 11 | Q 12 | } -------------------------------------------------------------------------------- /lib/src/utils.dart: -------------------------------------------------------------------------------- 1 | String addZero(String m){ 2 | return int.parse(m) < 9 ? '0$m' : m; 3 | } 4 | 5 | Map units = { 6 | 'Y': 'year', 7 | 'M': 'month', 8 | 'W': 'week', 9 | 'D': 'day', 10 | 'H': 'hour', 11 | 'MIN': 'minute', 12 | 'S': 'second', 13 | 'DATE': 'date', 14 | 'MS': 'millisecond', 15 | 'Q': 'quarter' 16 | }; -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://www.dartlang.org/tools/pub/glossary#lockfile 3 | packages: 4 | async: 5 | dependency: transitive 6 | description: 7 | name: async 8 | url: "https://pub.flutter-io.cn" 9 | source: hosted 10 | version: "2.0.8" 11 | boolean_selector: 12 | dependency: transitive 13 | description: 14 | name: boolean_selector 15 | url: "https://pub.flutter-io.cn" 16 | source: hosted 17 | version: "1.0.4" 18 | charcode: 19 | dependency: transitive 20 | description: 21 | name: charcode 22 | url: "https://pub.flutter-io.cn" 23 | source: hosted 24 | version: "1.1.2" 25 | collection: 26 | dependency: transitive 27 | description: 28 | name: collection 29 | url: "https://pub.flutter-io.cn" 30 | source: hosted 31 | version: "1.14.11" 32 | flutter: 33 | dependency: "direct main" 34 | description: flutter 35 | source: sdk 36 | version: "0.0.0" 37 | flutter_test: 38 | dependency: "direct dev" 39 | description: flutter 40 | source: sdk 41 | version: "0.0.0" 42 | matcher: 43 | dependency: transitive 44 | description: 45 | name: matcher 46 | url: "https://pub.flutter-io.cn" 47 | source: hosted 48 | version: "0.12.3+1" 49 | meta: 50 | dependency: transitive 51 | description: 52 | name: meta 53 | url: "https://pub.flutter-io.cn" 54 | source: hosted 55 | version: "1.1.6" 56 | path: 57 | dependency: transitive 58 | description: 59 | name: path 60 | url: "https://pub.flutter-io.cn" 61 | source: hosted 62 | version: "1.6.2" 63 | pedantic: 64 | dependency: transitive 65 | description: 66 | name: pedantic 67 | url: "https://pub.flutter-io.cn" 68 | source: hosted 69 | version: "1.4.0" 70 | quiver: 71 | dependency: transitive 72 | description: 73 | name: quiver 74 | url: "https://pub.flutter-io.cn" 75 | source: hosted 76 | version: "2.0.1" 77 | sky_engine: 78 | dependency: transitive 79 | description: flutter 80 | source: sdk 81 | version: "0.0.99" 82 | source_span: 83 | dependency: transitive 84 | description: 85 | name: source_span 86 | url: "https://pub.flutter-io.cn" 87 | source: hosted 88 | version: "1.5.4" 89 | stack_trace: 90 | dependency: transitive 91 | description: 92 | name: stack_trace 93 | url: "https://pub.flutter-io.cn" 94 | source: hosted 95 | version: "1.9.3" 96 | stream_channel: 97 | dependency: transitive 98 | description: 99 | name: stream_channel 100 | url: "https://pub.flutter-io.cn" 101 | source: hosted 102 | version: "1.6.8" 103 | string_scanner: 104 | dependency: transitive 105 | description: 106 | name: string_scanner 107 | url: "https://pub.flutter-io.cn" 108 | source: hosted 109 | version: "1.0.4" 110 | term_glyph: 111 | dependency: transitive 112 | description: 113 | name: term_glyph 114 | url: "https://pub.flutter-io.cn" 115 | source: hosted 116 | version: "1.1.0" 117 | test_api: 118 | dependency: transitive 119 | description: 120 | name: test_api 121 | url: "https://pub.flutter-io.cn" 122 | source: hosted 123 | version: "0.2.2" 124 | typed_data: 125 | dependency: transitive 126 | description: 127 | name: typed_data 128 | url: "https://pub.flutter-io.cn" 129 | source: hosted 130 | version: "1.1.6" 131 | vector_math: 132 | dependency: transitive 133 | description: 134 | name: vector_math 135 | url: "https://pub.flutter-io.cn" 136 | source: hosted 137 | version: "2.0.8" 138 | sdks: 139 | dart: ">=2.1.0 <3.0.0" 140 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: flutter_daydart 2 | description: 具备 Moment.js 一样 API 的时间处理库. 3 | version: 0.0.3 4 | author: icepy 5 | homepage: https://github.com/icepy/flutter_daydart 6 | repository: https://github.com/icepy/flutter_daydart 7 | environment: 8 | sdk: ">=2.0.0-dev.68.0 <3.0.0" 9 | 10 | dependencies: 11 | flutter: 12 | sdk: flutter 13 | 14 | dev_dependencies: 15 | flutter_test: 16 | sdk: flutter 17 | 18 | # For information on the generic Dart part of this file, see the 19 | # following page: https://www.dartlang.org/tools/pub/pubspec 20 | 21 | # The following section is specific to Flutter. 22 | flutter: 23 | 24 | # To add assets to your package, add an assets section, like this: 25 | # assets: 26 | # - images/a_dot_burr.jpeg 27 | # - images/a_dot_ham.jpeg 28 | # 29 | # For details regarding assets in packages, see 30 | # https://flutter.io/assets-and-images/#from-packages 31 | # 32 | # An image asset can refer to one or more resolution-specific "variants", see 33 | # https://flutter.io/assets-and-images/#resolution-aware. 34 | 35 | # To add custom fonts to your package, add a fonts section here, 36 | # in this "flutter" section. Each entry in this list should have a 37 | # "family" key with the font family name, and a "fonts" key with a 38 | # list giving the asset and other descriptors for the font. For 39 | # example: 40 | # fonts: 41 | # - family: Schyler 42 | # fonts: 43 | # - asset: fonts/Schyler-Regular.ttf 44 | # - asset: fonts/Schyler-Italic.ttf 45 | # style: italic 46 | # - family: Trajan Pro 47 | # fonts: 48 | # - asset: fonts/TrajanPro.ttf 49 | # - asset: fonts/TrajanPro_Bold.ttf 50 | # weight: 700 51 | # 52 | # For details regarding fonts in packages, see 53 | # https://flutter.io/custom-fonts/#from-packages 54 | -------------------------------------------------------------------------------- /test/flutter_daydart_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:flutter_test/flutter_test.dart'; 2 | import 'package:flutter_daydart/flutter_daydart.dart'; 3 | import 'package:flutter_daydart/src/Units.dart'; 4 | 5 | 6 | void main(){ 7 | final daydart = new DayDart(); 8 | final daydart2 = DayDart.fromDateTime(new DateTime.now()); 9 | final now = new DateTime.now(); 10 | final daydart3 = DayDart.fromString('2019-02-21'); 11 | final daydart4 = DayDart.fromString('2018-02-21'); 12 | 13 | test('format-> 测试格式化YYYY-MM-DD', (){ 14 | final fm = daydart3.format(fm: 'YYYY-MM-DD'); 15 | expect(fm, '2019-02-21'); 16 | }); 17 | 18 | test('isAfterValue-> 测试2018在2019之后', () { 19 | bool isD = daydart3.isAfterValue(2018, Units.Y); 20 | expect(isD, true); 21 | }); 22 | 23 | test('isBeforeValue-> 测试2020在2019之前', (){ 24 | bool isD = daydart3.isBeforeValue(2020, Units.Y); 25 | expect(isD, true); 26 | }); 27 | 28 | test('isSameValue-> 测试2019是否等于当前的年', (){ 29 | bool isD = daydart3.isSameValue(2019, Units.Y); 30 | expect(isD, true); 31 | }); 32 | 33 | test('static isDayDart-> 测试传入一个对象是否是 DayDart', (){ 34 | expect(DayDart.isDayDart(daydart), true); 35 | }); 36 | } 37 | --------------------------------------------------------------------------------