4

【笔记】Dart 的时间和日期

 1 year ago
source link: https://loli.fj.cn/2023/08/10/Dart%E7%9A%84%E6%97%B6%E9%97%B4%E5%92%8C%E6%97%A5%E6%9C%9F/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【笔记】Dart 的时间和日期

2023-08-10

Dart 的时间和日期学习笔记

根据当前时间创建时间日期对象

DateTime.now();

根据指定时间创建时间日期对象

DateTime("1970");
DateTime("1970", "01");
DateTime("1970", "01", "01");

根据指定时间创建时间日期对象(UTC)

DateTime.utc("1970");
DateTime.utc("1970", "01");
DateTime.utc("1970", "01", "01");

通过时间毫秒值创建时间日期对象

<milliseconds>:毫秒值

DateTime.fromMillisecondsSinceEpoch(<milliseconds>, isUtc: true);

时间日期对象与字符串互转

将字符串转换为时间日期对象

DateTime date = DateTime.parse("2000-01-01");
  • ISO 格式字符串
DateTime date = DateTime.parse('2000-01-01T00:00:00Z');

将时间日期对象转换为字符串

date:时间日期对象

String str = "${date.year.toString()}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')} ${date.hour.toString().padLeft(2, '0')}:${date.minute.toString().padLeft(2, '0')}"

知乎 —— 早起的年轻人


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK