Flutter intl包不正确地格式化分钟。

huangapple go评论65阅读模式
英文:

Flutter intl package formats minutes incorrectly

问题

以下是您提供的代码部分的翻译:

给定的代码返回一个时间,但时间差了几分钟(请注意,我正在运行在美国中部的夏令时):

final givenDate = DateTime.parse('1980-01-03T11:05:16Z');
const expectedDateString = '01/03/1980 11:05:16';

print("givenDate is ${givenDate.toString()}");
print(
    "DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is ${DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate)}");

打印输出如下:

givenDate is 1980-01-03 11:05:16.000Z

DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is 01/03/1980 11:01:16

相差了四分钟。格式化后的日期/时间应为 01/03/1980 11:05:16

更改常量后:

final givenDate = DateTime.parse('1980-01-03T11:45:16.735Z');
const expectedDateString = '01/03/1980 11:45:16';

结果如下:

givenDate is 1980-01-03 11:45:16.735Z

DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is 01/03/1980 11:01:16

分钟部分仍然是 '01'。它应该是 '45'。

我正在运行:

Flutter 版本 3.10.0(稳定渠道)

Dart 版本 3.0.0

intl: ^0.18.1(与 0.18.0 版本出现相同的问题)

英文:

The following code returns a date that is off by several minutes (note that I am running in US Central DST):

      final givenDate = DateTime.parse('1980-01-03T11:05:16Z');
      const expectedDateString = '01/03/1980 11:05:16';

      print("givenDate is ${givenDate.toString()}");
      print(
          "DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is ${DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate)}");

The prints show:

givenDate is 1980-01-03 11:05:16.000Z

DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is 01/03/1980 11:01:16

Four minutes off. The formatted date/time should be 01/03/1980 11:05:16

Changing the constants:

      final givenDate = DateTime.parse('1980-01-03T11:45:16.735Z');
      const expectedDateString = '01/03/1980 11:45:16';

Results in:

givenDate is 1980-01-03 11:45:16.735Z

DateFormat('MM/dd/yyyy HH:MM:ss').format(givenDate) is 01/03/1980 11:01:16

The minutes portion is still '01'. It should be 45.

I am running:

Flutter version 3.10.0 on channel stable

Dart version 3.0.0

intl: ^0.18.1 (same issue with 0.18.0)

答案1

得分: 0

Your format is incorrect. MM means month and mm means minute. (You are printing the month not the minute in the time.)

Change DateFormat('MM/dd/yyyy HH:MM:ss') to DateFormat('MM/dd/yyyy HH:mm:ss')

英文:

Your format is incorrect. MM means month and mm means minute. (You are printing the month not the minute in the time.)

Change DateFormat('MM/dd/yyyy HH:MM:ss') to DateFormat('MM/dd/yyyy HH:mm:ss')

huangapple
  • 本文由 发表于 2023年5月18日 05:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定