DateTime 類
??DateTime類用來標(biāo)識(shí)一個(gè)瞬時(shí)的時(shí)間節(jié)點(diǎn),可以通過構(gòu)造函數(shù),從標(biāo)準(zhǔn)格式(符合ISO 8601標(biāo)準(zhǔn))的字符串中構(gòu)造一個(gè)時(shí)間對(duì)象。DateTIme使用24小時(shí)計(jì)時(shí)法。以下是最基礎(chǔ)的例子:
var now = new DateTime.now();
var berlinWallFell = new DateTime.utc(1989, 11, 9);
var moonLanding = DateTime.parse("1969-07-20 20:18:04Z"); // 8:18pm
DateTime對(duì)象會(huì)錨定UTC(通用協(xié)調(diào)時(shí)Universal Time Coordinated)時(shí)區(qū)或者設(shè)備的本地時(shí)區(qū)。在創(chuàng)建之后,DateTime的值和所屬的時(shí)區(qū)都不會(huì)改變??梢酝ㄟ^對(duì)象屬性讀取具體的時(shí)間值:
assert(berlinWallFell.month == 11); // 柏林墻倒塌的月份
assert(moonLanding.hour == 20); // 登月時(shí)的時(shí)間(小時(shí))
出于便捷性和可讀性的考量,DateTIme類提供了星期和月份的常量值可供調(diào)用,你可以使用很多常量來提高代碼的可讀性,示例如下:
var berlinWallFell = new DateTime.utc(1989, DateTime.november, 9);
assert(berlinWallFell.weekday == DateTime.thursday);
星期和月份的常量都是從1開始的,每周的開始從周一開始計(jì)算,所以january和monday的值都是1。
構(gòu)造方法
DateTime(int year, [ int month = 1 int day = 1 int hour = 0 int minute = 0 int second = 0 int millisecond = 0 int microsecond = 0 ])
該方法創(chuàng)建一個(gè)依托于本地時(shí)區(qū)的DateTime實(shí)例
DateTime.fromMicrosecondsSinceEpoch(int microsecondsSinceEpoch, { bool isUtc: false })
該方法輸入距離1970年1月1日0時(shí)0分0秒的微秒數(shù),得到一個(gè)DateTime實(shí)例。
DateTime.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch, { bool isUtc: false })
該方法輸入距離1970年1月1日0時(shí)0分0秒的毫秒數(shù),得到一個(gè)DateTime實(shí)例。
DateTime.now()
該方法返回一個(gè)依托于本地時(shí)區(qū)的代表當(dāng)前時(shí)間的DateTime實(shí)例。
DateTime.utc(int year, [ int month = 1 int day = 1 int hour = 0 int minute = 0 int second = 0 int millisecond = 0 int microsecond = 0 ])
該方法返回一個(gè)依托于UTC的DateTime實(shí)例
實(shí)例方法
add(Duration duration) → DateTime
加上傳入的Duration實(shí)例所表示的時(shí)間間隔,返回一個(gè)新的DateTime實(shí)例,新實(shí)例與調(diào)用實(shí)例之間的時(shí)間差為duration
var now = DateTime.now();
var span = Duration(days: 1);
print(now);
print(now.add(span));
輸出:
2019-12-15 09:29:59.605510
2019-12-16 09:29:59.605510
compareTo(DateTime other) → int
比較兩個(gè)DateTime實(shí)例,如果二者相等,則返回0
difference(DateTime other) → Duration
返回當(dāng)前實(shí)例與傳入實(shí)例之間的時(shí)間差,返回一個(gè)Duration實(shí)例
isAfter(DateTime other) → bool
同傳入的DateTime進(jìn)行比較,如果標(biāo)識(shí)的時(shí)間在傳入實(shí)例之后,則返回true
isAtSameMomentAs(DateTime other) → bool
同傳入的DateTime進(jìn)行比較,如果二者表示的時(shí)間在同一時(shí)刻,則返回true
isBefore(DateTime other) → bool
同傳入的DateTime進(jìn)行比較,如果標(biāo)識(shí)的時(shí)間在傳入實(shí)例之前,則返回true
subtract(Duration duration) → DateTime
減去傳入的Duration實(shí)例所表示的時(shí)間間隔,返回一個(gè)新的DateTime實(shí)例,新實(shí)例與調(diào)用實(shí)例之間的時(shí)間差為duration
var now = DateTime.now();
var span = Duration(days: 1);
print(now);
print(now.subtract(span));
輸出:
2019-12-15 09:32:02.811829
2019-12-14 09:32:02.811829
toIso8601String() → String
返回依照IOS-8601標(biāo)準(zhǔn)的全精度表示的時(shí)間格式
toLocal() → DateTime
返回該實(shí)例在本地時(shí)區(qū)下表示的實(shí)例
toString() → String
返回該實(shí)例的字符串表現(xiàn)
toUtc() → DateTime
返回該實(shí)例在UTC時(shí)間表示下的實(shí)例
操作符
operator ==(dynamic other) → bool
只有當(dāng)傳入值為DateTime實(shí)例并且表示的時(shí)間相同且在同一時(shí)區(qū)下時(shí)返回true
靜態(tài)方法
parse(String formattedString) → DateTime
通過標(biāo)準(zhǔn)格式的字符串來構(gòu)造一個(gè)DateTime實(shí)例
var now = DateTime.parse('2019-12-12');
print(now);
輸出:
2019-12-12 00:00:00.000
tryParse(String formattedString) → DateTime
通過標(biāo)準(zhǔn)格式的字符串來構(gòu)造一個(gè)DateTime實(shí)例
實(shí)例屬性
day → int
返回實(shí)例的日期,從取值范圍從1到30
hashCode → int
返回實(shí)例對(duì)象的hash碼
hour → int
返回在24小時(shí)計(jì)時(shí)法中的小時(shí)時(shí)間,取值范圍從0到23
isUtc → bool
返回當(dāng)前實(shí)例是否是UTC計(jì)時(shí)
microsecond
返回實(shí)例表示的時(shí)間距離1970年1月1日0時(shí)0分0秒的微秒數(shù)的最后三位數(shù),取值范圍0到999
microsecondsSinceEpoch → int
返回實(shí)例表示的時(shí)間距離1970年1月1日0時(shí)0分0秒的微秒數(shù)
millisecond → int
返回實(shí)例表示的時(shí)間距離1970年1月1日0時(shí)0分0秒的毫秒數(shù)的最后三位數(shù),取值范圍0到999
millisecondsSinceEpoch → int
返回實(shí)例表示的時(shí)間距離1970年1月1日0時(shí)0分0秒的毫秒數(shù)
minute → int
返回實(shí)例表示時(shí)間的分鐘數(shù),取值范圍從0到59
month → int
返回實(shí)例表示時(shí)間的月份,取值范圍從1到12
second → int
返回實(shí)例表示時(shí)間的月份,取值范圍從0到59
timeZoneName → String
返回實(shí)例的時(shí)區(qū)名
timeZoneOffset → Duration
返回當(dāng)前時(shí)區(qū)與UTC時(shí)間之間的時(shí)間間隔,用Duration對(duì)象來表示
weekday → int
返回實(shí)例表示時(shí)間是星期幾,取值范圍從1到7
year → int
返回實(shí)例表示時(shí)間的年份
使用UTC時(shí)區(qū)和本地時(shí)區(qū)
DateTime實(shí)例默認(rèn)使用本地時(shí)區(qū),除非在創(chuàng)建時(shí)顯示聲明使用UTC時(shí)區(qū)。
var dDay = new DateTime.utc(1944, 6, 6);
可以使用isUtc來確定當(dāng)前的時(shí)間是否是UTC時(shí)區(qū)??梢允褂?code>toLocal和toUtc來進(jìn)行時(shí)區(qū)間的轉(zhuǎn)換,使用timeZoneName來獲取DateTime實(shí)例所屬時(shí)區(qū)的簡寫,使用timeZoneOffset來獲取不同時(shí)區(qū)之間的時(shí)間差值。
DateTime實(shí)例之間的比較
DateTime類有幾個(gè)簡單的方法來實(shí)現(xiàn)DateTime實(shí)例之間的比較,比如isAfter、isBefore、isAtSameMomentAs:
assert(berlinWallFell.isAfter(moonLanding) == true);
assert(berlinWallFell.isBefore(moonLanding) == false);
DateTime與Duration類的配合使用
可以通過add和subtract方法加減一個(gè)Duration對(duì)象來對(duì)DateTime實(shí)例進(jìn)行操作,并返回一個(gè)新的實(shí)例。例如,計(jì)算六天后的今天的時(shí)間:
var now = new DateTime.now();
var sixtyDaysFromNow = now.add(new Duration(days: 60));
為了找出兩個(gè)DateTime實(shí)例之間的差值,我們可以使用difference方法,它將返回一個(gè)Duration實(shí)例:
var difference = berlinWallFell.difference(moonLanding);
assert(difference.inDays == 7416);
兩個(gè)不同時(shí)區(qū)之間的時(shí)間差就是按照兩地之間的時(shí)間納秒差,這個(gè)結(jié)果并不會(huì)補(bǔ)償日歷日或時(shí)制之間的差別。這就意味著如果相鄰兩個(gè)午夜時(shí)間跨過了夏令時(shí)的變更日期,那么這兩個(gè)午夜之間的時(shí)間差可能不滿24個(gè)小時(shí)。上述代碼中的時(shí)間差如果用澳大利亞本地時(shí)間計(jì)算,那么結(jié)果將為7415天又23小時(shí),按照整天來計(jì)算只有7415天。
其他資源
關(guān)于時(shí)間區(qū)間(span of time)的表示方法,詳見Duration,關(guān)于時(shí)間間隔的表示方法,詳見Stopwatch,DateTime類并沒有提供國際化的相關(guān)功能。為了進(jìn)行國際化,可以使用intl包。
常量
april → const int
4
august → const int
8
daysPerWeek → const int
7
december → const int
12
february → const int
2
friday → const int
5
january → const int
1
july → const int
7
june → const int
6
march → const int
3
may → const int
5
monday → const int
1
monthsPerYear → const int
12
november → const int
11
october → const int
10
saturday → const int
6
september → const int
9
sunday → const int
7
thursday → const int
4
tuesday → const int
2
wednesday → const int
3
純手工翻譯,感謝支持!