Dart-从字符串中提取日期(MM / DD / YYYY)

我正在Firebase中工作,并使用JavaScript编写了Cloud Function。我正在尝试从Firebase时间戳中提取“年月日”字符串(从我的数据库中)。

In my Cloud Function I use "timestampObj.toDate().toString()" and it returns a string like this in my client-side: Fri Dec 06 2019 18:06:52 GMT+0000 (UTC)

如何从我的Dart代码中的上一行获取字符串“ 2019年12月9日”?

评论
  • 菊花残
    菊花残 回复

    Converting the DateTime returned by toDate() into a String sounds like putting yourself through much more trouble than necessary. DateTime has lots of methods for easily extracting the calendar components out of it. You can see from that API doc link that it has properties for year, month, and day that require no text parsing. It should be trivial to convert those into the string format you want.

    Even better, you can just use the DateFormat class.