请参见以下代码:
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方法支持微秒。
答案 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个构造函数fromMicrosecondsSinceEpoch
和fromMillisecondsSinceEpoch
,用于从整数值创建实例。
答案 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