[英]Flutter/Dart: Convert date to yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
Trying to convert DateTime.now()
to this: 2019-06-04T12:08:56.235-0700
尝试将DateTime.now()
转换为: 2019-06-04T12:08:56.235-0700
The API documentation insists that's Data format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
but I can't figure it out for Dart. API 文档坚持认为这是数据格式: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
但我无法为 Dart 弄清楚。 Not even python for that matter.甚至没有python。
I've tried now.toIso8601String()
but it doesn't quite nail it (ie 2019-12-03T17:50:23.476164
).我已经尝试过now.toIso8601String()
但它并没有完全确定它(即2019-12-03T17:50:23.476164
)。 It's missing the last -
section and one digit.它丢掉了最后-
段和一个数字。 The API call within flutter returns a {code:415 message HTTP 415 Unsupported Media Type}
as a result.因此,flutter 中的 API 调用会返回{code:415 message HTTP 415 Unsupported Media Type}
。
What am I missing?我错过了什么?
It seems, as for intl/intl.dart
DateFormat class, time zone has not been implemented yet.看来,对于intl/intl.dart
DateFormat类,时区还没有实现。
The following characters are reserved and currently are unimplemented:以下字符是保留的,目前未实现:
Symbol象征 | Meaning意义 | Presentation介绍 | Example例子 |
---|---|---|---|
z z | time zone时区 | (Text) (文本) | Pacific Standard Time太平洋标准时间 |
Z Z | time zone (RFC 822)时区 (RFC 822) | (Number) (数字) | -0800 -0800 |
v v | time zone (generic)时区(通用) | (Text) (文本) | Pacific Time太平洋时间 |
Like @hata pointed out, it's not yet implemented.就像@hata 指出的那样,它还没有实施。 So, to add the Z (timezone as number) exactly as requested I did this:因此,要完全按照要求添加 Z(时区为数字),我这样做了:
String formatISOTime(DateTime date) {
//converts date into the following format:
// or 2019-06-04T12:08:56.235-0700
var duration = date.timeZoneOffset;
if (duration.isNegative)
return (DateFormat("yyyy-MM-ddTHH:mm:ss.mmm").format(date) +
"-${duration.inHours.toString().padLeft(2, '0')}${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
else
return (DateFormat("yyyy-MM-ddTHH:mm:ss.mmm").format(date) +
"+${duration.inHours.toString().padLeft(2, '0')}${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}
Implemented:实施的:
void main() {
DateTime getCurrentTimestamp = DateTime.now();
print('agreement signed');
String agreementSigned = formatISOTime(getCurrentTimestamp);
print(agreementSigned);
}
When ran:运行时:
main();
// Result:
2021-10-29T17:53:56.053+0100
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.