简体   繁体   English

Flutter/Dart:将日期转换为 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

[英]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.

相关问题 Flutter/Dart:解析 'yyyy-MM-dd HH:MM:SS' 产生错误的 output? - Flutter/Dart : Parsing 'yyyy-MM-dd HH:MM:SS' producing wrong output? 如何在 Dart/Flutter 中将“EEE, MMM d, yyyy hh:mm aaa”日期时间格式转换为“yyyy-MM-dd HH:mm:ss”日期时间格式? - How To Convert "EEE, MMM d, yyyy hh:mm aaa" DateTime Format to "yyyy-MM-dd HH:mm:ss" DateTime Format in Dart/Flutter? 日期时间格式为 Flutter dd/MM/YYYY hh:mm - Date Time format in Flutter dd/MM/YYYY hh:mm Dart - 将时间从 dd/MM/YYYY 转换为 YYYY-MM-dd - Dart - Convert time from dd/MM/YYYY to YYYY-MM-dd Dart - 如何将 yyyy-MM-dd 中的简单日期字符串的格式更改为 dd-MM-yyyy - Dart - How to change format of simple date string which is in yyyy-MM-dd to dd-MM-yyyy 如何解析日期和时间 2020-07-13 12:02:22.952999+0700 以格式化 yyyy-MM-dd HH:mm:ss - How to parse date and time 2020-07-13 12:02:22.952999+0700 to format yyyy-MM-dd HH:mm:ss Flutter 如何将字符串日期转换为 dd/MM/yyyy - Flutter how to convert string date to dd/MM/yyyy 如何将时间字符串(YYYY-MM-DD)转换为 Dart 中剩余的 xx 天 xx 小时 xx 分钟? - how to convert time string (YYYY-MM-DD) into xx Day xx Hr xx min remaining in Dart? 将日历日期时间与颤振中的 yyyy-mm-dd 格式进行比较 - Comparing calendar datetime to yyyy-mm-dd format in flutter 在 Flutter 我得到了一个 DateTime [yyyy-MM-dd 00:00:00.000] 我如何才能将它转移为仅 [yyyy-MM-dd]? - In Flutter i got a DateTime [yyyy-MM-dd 00:00:00.000] how can i transfer that to be [yyyy-MM-dd] only?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM