Dart-将自纪元以来的毫秒数(UNIX时间戳)转换为人类可读的时间
问题描述:
从纪元(例如1486252500000 13位数字)将时间格式化为人类可读格式以来,是否存在一种解析毫秒的好方法?
Is there a good way to parse milliseconds since epoch (ex. 1486252500000 13 digits) formatted time into a human readable format?
答
DateTime
确实有一个自时期以来毫秒的命名构造函数
DateTime
does have a named constructor for millisecond since epoch
https://api.dartlang.org/stable/1.24.2/dart-core/DateTime/DateTime.fromMillisecondsSinceEpoch.html
DateTime date = new DateTime.fromMillisecondsSinceEpoch(1486252500000)
如果要将其转换为人类可读的字符串,可以使用 intl 包和 DateFormat 类
If you want to convert it to human readable string, you can use intl package with the DateFormat class
import "package:intl/intl_browser.dart";
var format = new DateFormat("yMd");
var dateString = format.format(date);