toDateStr method

String toDateStr({
  1. String format = 'yyyy-MM-dd HH:mm:ss',
})

毫秒时间戳转为日期字符串

format 字符串格式,默认为 'yyyy-MM-dd HH:mm:ss'。

返回结果: 格式化后的时间字符串。

示例:

int timestamp = 1710441600000; // 2024-03-15 00:00:00
print(timestamp.toDateStr(format: 'yyyy-MM-dd'));
// 输出: '2024-03-15'

Implementation

String toDateStr({String format = 'yyyy-MM-dd HH:mm:ss'}) {
  final dateTime = DateTime.fromMillisecondsSinceEpoch(this);
  return DateFormat(format).format(dateTime);
}