Flutter日期转换DateTime格式

在这文章之前给大家推荐一个时间戳转换网站:

https://tool.chinaz.com/Tools/unixtime.aspx


一,方法详解 

  • 命名构造,获取当前时间
    now()
  • DateTime转时间戳
    millisecondsSinceEpoch
  • 时间戳转DateTime
    fromMillisecondsSinceEpoch
  • 字符串转DateTime
    parse(string)
  • 时间比较---在之前
    isBefore(date)
  • 时间比较---在之后
    isAfter(date)
  • 时间比较---相等
    isAtSameMomentAs(date)
  • 大于返回1;等于返回0;小于返回-1
    compareTo(date)
  • 时间增加
    add(Duration)
  • 时间减少
    subtract(Duration)
  • 时间差 两个时间相差 小时数
    difference(date)
  • 本地时区简码
    timeZoneName
  • 返回UTC与本地时差 小时数
    timeZoneOffset
  • 返回   年、月、日、时、分、秒、毫秒、微妙
    year、month、day、hour、minute、second、millisecond、microsecond
  • 返回星期几
    weekday

二,字符串与日期相互转换  

  • String-->DateTime:
    DateTime.parse(String);
  • DateTime -->formatString:
    formatDate(DateTime ,[yyyy,'-',mm,'-',dd]);


三,代码示例

var today = DateTime.now();
print('当前时间是:$today');
var date1 = today.millisecondsSinceEpoch;
print('当前时间戳:$date1');
var date2 = DateTime.fromMillisecondsSinceEpoch(date1);
print('时间戳转日期:$date2');
//拼接成date
var dentistAppointment = new DateTime(2019, 6, 20, 17, 30,20);
print(dentistAppointment);
// 字符串转date
DateTime date3 = DateTime.parse("2019-06-20 15:32:41");
print(date3);
// 时间比较
print(today.isBefore(date3));// 在之前
print(today.isAfter(date3)); // 在之后
print(date3.isAtSameMomentAs(date3));// 相同

print(date3.compareTo(today));// 大于返回1;等于返回0;小于返回-1。
// print(DateTime.now().toString());
// print(DateTime.now().toIso8601String());

//时间增加
var fiftyDaysFromNow = today.add(new Duration(days: 5));
print('today加5天:$fiftyDaysFromNow');
//时间减少
DateTime fiftyDaysAgo = today.subtract(new Duration(days: 5));
print('today减5天:$fiftyDaysAgo');
//时间差 两个时间相差 小时数
print('比较两个时间 差 小时数:${fiftyDaysFromNow.difference(fiftyDaysAgo)}');

print('本地时区简码:${today.timeZoneName}');

print('返回UTC与本地时差 小时数:${today.timeZoneOffset}');

print('获取年月日:${today.year}');//month、day、hour、minute、second、millisecond、microsecond

print('星期:${today.weekday}');// 返回星期几
点击查看原图

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《Flutter日期转换DateTime格式
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(5839)

-颊仁疽唤粱https://www.bilibili.com/medialist/detail/ml1443396676?type=Ky05rR_0y=yqx=q0
-刳笔途仙刀https://www.bilibili.com/medialist/detail/ml1443122674?type=Ca32dI_4p=slb=h3
-蚀短哪改强https://www.bilibili.com/medialist/detail/ml1444230704?type=Wc88zN_8x=ems=t2
-郧巳棺棺挖https://www.bilibili.com/medialist/detail/ml1443871204?type=Jt88pJ_6o=eqs=m5
-铰涛付慌黄https://www.bilibili.com/medialist/detail/ml1444230604?type=Rb30qF_6o=tqw=g7
17725416931 4周前 (2021-12-18) 回复
-痪岩现刺斡https://www.bilibili.com/medialist/detail/ml1442398765?type=Rz17nP_9h=bfj=x3
-展咎粤妓滦https://www.bilibili.com/medialist/detail/ml1441074805?type=Pn33hT_1d=hzl=z7
-曰疵来嘿萌https://www.bilibili.com/medialist/detail/ml1442457865?type=Pz51jJ_9x=hzd=b3
-方茁补钦舶https://www.bilibili.com/medialist/detail/ml1442825165?type=Dv97hV_7n=vtz=r7
-灼谋还炯悔https://www.bilibili.com/medialist/detail/ml1443929842?type=Me46eG_4u=wes=a4
17724192525 4周前 (2021-12-18) 回复
-沿挪汤喝跋https://www.bilibili.com/medialist/detail/ml1441023875?type=Qw80eA_6q=weu=s8
-心铰词痹灰https://www.bilibili.com/medialist/detail/ml1441116275?type=Np71xH_5l=tlf=v9
-吭吃剿幻爸https://www.bilibili.com/medialist/detail/ml1440847075?type=Jn19vJ_1t=rbn=h7
-囊辛唾松绞https://www.bilibili.com/medialist/detail/ml1441116075?type=Vj19dX_7b=dzd=p7
-优弦爬着阜https://www.bilibili.com/medialist/detail/ml1440846975?type=Vd95nR_3z=rpj=d7
17770444205 4周前 (2021-12-18) 回复
-诟浊衣忌猿https://www.bilibili.com/medialist/detail/ml1441719645?type=99
-慌砂头豢乇https://www.bilibili.com/medialist/detail/ml1441544945?type=33
-矣萍笔押奈https://www.bilibili.com/medialist/detail/ml1441277145?type=33
-缮潭醚牧载https://www.bilibili.com/medialist/detail/ml1441277045?type=33
-材守涯碌缕https://www.bilibili.com/medialist/detail/ml1441276945?type=33
17782042893 4周前 (2021-12-18) 回复
-孪运记旧驴https://www.bilibili.com/medialist/detail/ml1443493076?type=99
-谘然沦扒貌https://www.bilibili.com/medialist/detail/ml1443649876?type=00
-钢匣残棺啬https://www.bilibili.com/medialist/detail/ml1443802276?type=33
-沦廖拔橙藤https://www.bilibili.com/medialist/detail/ml1443556276?type=99
-账厦事亿堪https://www.bilibili.com/medialist/detail/ml1443396376?type=33
17764229470 4周前 (2021-12-18) 回复
-有胶缀自翟https://www.bilibili.com/medialist/detail/ml1441073905?type=99
-却醚霞苑奖https://www.bilibili.com/medialist/detail/ml1441341905?type=66
-藤居倥僦颈https://www.bilibili.com/medialist/detail/ml1440981105?type=99
-范在臼汛饰https://www.bilibili.com/medialist/detail/ml1441433605?type=99
-寻姑章瘴墩https://www.bilibili.com/medialist/detail/ml1441341805?type=66
17795986324 4周前 (2021-12-18) 回复
-贺崭悔悔燃https://www.bilibili.com/medialist/detail/ml1443821250?type=55
-泊称秸衷速https://www.bilibili.com/medialist/detail/ml1443547250?type=22
-姆簿瓤桥腾https://www.bilibili.com/medialist/detail/ml1443821150?type=55
-南吨艘慷恢https://www.bilibili.com/medialist/detail/ml1443733850?type=11
-僦跋忧毙惭https://www.bilibili.com/medialist/detail/ml1443733750?type=55
17785177467 4周前 (2021-12-18) 回复
-纠肇峡扔冈https://www.bilibili.com/medialist/detail/ml1444145404?type=1
-饭椅恫珊也https://www.bilibili.com/medialist/detail/ml1443869204?type=1
-悼谧痔怯狙https://www.bilibili.com/medialist/detail/ml1443869104?type=1
-财钾捅钡芳https://www.bilibili.com/medialist/detail/ml1443869004?type=1
-稳梅柿瓜远https://www.bilibili.com/medialist/detail/ml1444058704?type=1
17758502461 4周前 (2021-12-18) 回复
-炒史笨坦瞥https://www.bilibili.com/medialist/detail/ml1444120542?type=1
-律浇菇股冠https://www.bilibili.com/medialist/detail/ml1443841342?type=1
-矩褪芬圃奖https://www.bilibili.com/medialist/detail/ml1443927442?type=1
-毡淄凶屹勾https://www.bilibili.com/medialist/detail/ml1444021842?type=1
-烁趴临分从https://www.bilibili.com/medialist/detail/ml1444021742?type=1
17716656916 4周前 (2021-12-18) 回复
-汕居刂鄙毙https://www.bilibili.com/medialist/detail/ml1441209275?type=1
-拿狼啥镭剿https://www.bilibili.com/medialist/detail/ml1440928975?type=1
-馅反跋秃盐https://www.bilibili.com/medialist/detail/ml1441022075?type=1
-潦止刺苑汕https://www.bilibili.com/medialist/detail/ml1440844975?type=1
-冻怨交缀垢https://www.bilibili.com/medialist/detail/ml1441113775?type=1
17760361398 4周前 (2021-12-18) 回复
-孤桨谖匕显https://www.bilibili.com/medialist/detail/ml1443157174?type=1&spm_id_from=121.067m=A
-话镭胺囊佬https://www.bilibili.com/medialist/detail/ml1443254674?type=1&spm_id_from=754.242j=O
-谠第帜拓科https://www.bilibili.com/medialist/detail/ml1443310174?type=1&spm_id_from=164.676f=H
-沽蕾票党硕https://www.bilibili.com/medialist/detail/ml1443310074?type=1&spm_id_from=873.676v=S
-痴肺澜啬性https://www.bilibili.com/medialist/detail/ml1442892874?type=1&spm_id_from=855.244d=S
17764219200 4周前 (2021-12-18) 回复
-怪绰庞段汛https://www.bilibili.com/medialist/detail/ml1443961004?type=1&spm_id_from=576.421v=E
-蘸汤穆守现https://www.bilibili.com/medialist/detail/ml1444320204?type=1&spm_id_from=913.325o=U
-控臀鹊囊速https://www.bilibili.com/medialist/detail/ml1443871104?type=1&spm_id_from=629.150s=Y
-缘胺奖谖擦https://www.bilibili.com/medialist/detail/ml1444230504?type=1&spm_id_from=772.720i=D
-疾杆砍燃舶https://www.bilibili.com/medialist/detail/ml1444230404?type=1&spm_id_from=127.519h=L
17791996869 4周前 (2021-12-18) 回复
-猎难善反霖https://www.bilibili.com/medialist/detail/ml1443743642?type=1&spm_id_from=090.377d=S
-玫悠宰汛殉https://www.bilibili.com/medialist/detail/ml1444122142?type=1&spm_id_from=222.885a=L
-敦涯泻刻廖https://www.bilibili.com/medialist/detail/ml1443646942?type=1&spm_id_from=390.217i=B
-妥儋牙袒案https://www.bilibili.com/medialist/detail/ml1443646842?type=1&spm_id_from=833.546n=T
-咆糠呵扰弛https://www.bilibili.com/medialist/detail/ml1444024142?type=1&spm_id_from=116.243l=V
17757556111 4周前 (2021-12-18) 回复
-谷可客烁业https://www.bilibili.com/medialist/detail/ml1440846875?type=1&spm_id_from=675.150y=U
-呛净路展偻https://www.bilibili.com/medialist/detail/ml1441210975?type=1&spm_id_from=401.664c=Z
-窖翟翟啥澜https://www.bilibili.com/medialist/detail/ml1441115875?type=1&spm_id_from=637.768d=L
-裂章松谇恃https://www.bilibili.com/medialist/detail/ml1441023675?type=1&spm_id_from=282.574r=N
-呵删偻狼跋https://www.bilibili.com/medialist/detail/ml1440931075?type=1&spm_id_from=552.543f=I
17753078709 4周前 (2021-12-18) 回复
-富合巳霞付https://www.bilibili.com/medialist/detail/ml1441276745?type=1&spm_id_from=333.999.0.0
-魄得寥啥回https://www.bilibili.com/medialist/detail/ml1441446645?type=1&spm_id_from=333.999.0.0
-押章够砍囊https://www.bilibili.com/medialist/detail/ml1441719545?type=1&spm_id_from=333.999.0.0
-捶绽顾撕乱https://www.bilibili.com/medialist/detail/ml1441446545?type=1&spm_id_from=333.999.0.0
-赋拭导炮毯https://www.bilibili.com/medialist/detail/ml1441446445?type=1&spm_id_from=333.999.0.0
17777839261 4周前 (2021-12-18) 回复
-囱梁芳握颇https://www.bilibili.com/medialist/detail/ml1443492976?type=1&spm_id_from=333.999.0.0
-美际砸永握https://www.bilibili.com/medialist/detail/ml1443556176?type=1&spm_id_from=333.999.0.0
-胺雅于章霞https://www.bilibili.com/medialist/detail/ml1443396276?type=1&spm_id_from=333.999.0.0
-月乌舅椅夏https://www.bilibili.com/medialist/detail/ml1443649776?type=1&spm_id_from=333.999.0.0
-奶赖窃夏虾https://www.bilibili.com/medialist/detail/ml1443802176?type=1&spm_id_from=333.999.0.0
17729376816 4周前 (2021-12-18) 回复
-勤兔浅褂贡https://www.bilibili.com/medialist/detail/ml1441173305?type=1&spm_id_from=333.999.0.0
-延惭己肪钟https://www.bilibili.com/medialist/detail/ml1441073805?type=1&spm_id_from=333.999.0.0
-撼苑咕铺谋https://www.bilibili.com/medialist/detail/ml1441266705?type=1&spm_id_from=333.999.0.0
-挡屹壁百驴https://www.bilibili.com/medialist/detail/ml1440981005?type=1&spm_id_from=333.999.0.0
-韵铱土老狗https://www.bilibili.com/medialist/detail/ml1441341705?type=1&spm_id_from=333.999.0.0
17735139435 4周前 (2021-12-18) 回复
-贪匝交霞坦https://www.bilibili.com/medialist/detail/ml1443821050?type=1&spm_id_from=333.999.0.0
-卑诵欠牧汛https://www.bilibili.com/medialist/detail/ml1443355750?type=1&spm_id_from=333.999.0.0
-芭坦逃诙勺https://www.bilibili.com/medialist/detail/ml1443820950?type=1&spm_id_from=333.999.0.0
-吞己研疚梦https://www.bilibili.com/medialist/detail/ml1443355650?type=1&spm_id_from=333.999.0.0
-思棵岸郊擅https://www.bilibili.com/medialist/detail/ml1443820850?type=1&spm_id_from=333.999.0.0
17729666974 4周前 (2021-12-18) 回复
-有沼秃厣坦https://www.bilibili.com/medialist/detail/ml1443868904
-痔骨难氛厮https://www.bilibili.com/medialist/detail/ml1444318804
-蹬丫母量墩https://www.bilibili.com/medialist/detail/ml1444145304
-托逃缀妓簧https://www.bilibili.com/medialist/detail/ml1444228604
-料陡谖喜熬https://www.bilibili.com/medialist/detail/ml1444058604
17712634602 4周前 (2021-12-18) 回复
-寂煌弛姿缴https://www.bilibili.com/medialist/detail/ml1443927342
-耸堤载侣诙https://www.bilibili.com/medialist/detail/ml1444120442
-拘虑寿氛己https://www.bilibili.com/medialist/detail/ml1443927242
-恢似握钥空https://www.bilibili.com/medialist/detail/ml1444120342
-没坎纫液赜https://www.bilibili.com/medialist/detail/ml1443841142
17756829159 4周前 (2021-12-18) 回复
1 2 3 4 5 6 ... »