Dart:DateTime.parse()不包括微秒

时间:2018-09-07 17:17:30

标签: dart

请参见以下代码:

void main() 
{
  String test = '2017-10-11T12:03:46.351363-04:00';
  DateTime testDate = DateTime.parse(test);
  print(testDate.microsecond);
  print(testDate.toString());
}

运行此代码时,我在解析的字符串中损失了微秒。为什么会这样,有解决办法吗?

Dart文档说parse方法支持微秒。

2 个答案:

答案 0 :(得分:0)

'2017-10-11T12:03:46.351363-04:00'不是Dart DateTime可以处理的格式。

DateTime仅支持Z用于GMT,而本地时间不支持Z

只需打印创建的DateTime中的值即可查看其可以解析的格式。

print(DateTime.now());
print(DateTime.now().toUtc())

DateTime还具有2个构造函数fromMicrosecondsSinceEpochfromMillisecondsSinceEpoch,用于从整数值创建实例。

答案 1 :(得分:0)

这里有个问题 https://github.com/dart-lang/co19/issues/17

如测试所示,它仅保留小数点后6位 https://github.com/dart-lang/co19/commit/8465825f60c9580d82ae01ffc040f3b589aaf667#diff-02c526d1dcb5aa2dcdab3500c14ede87R40

您可以解析格式>> start = int(input("Enter the start of range: ")) >> end = int(input("Enter the end of range: ")) >> tot = [n for n in range(start, end+1) if n%2] or [n for n in range(start, end+1) if not n%2] >> print("your odd numbers are") >> print(tot, end = "") >> print ("and their sum is their sum is") >> print(sum(tot))`

但无法解析2018-12-11T12:00:32.304272Z