将字符串转换为颤动的DateTime

问题描述 投票:0回答:1

我有此代码

if (obj.due_date != null) {
      print('The date is  '+obj.due_date);
      print('now change to ' +
       DateUtil().formattedDate(DateTime.parse(obj.due_date)));
 }

DateUtil

import 'package:intl/intl.dart';

class DateUtil {
  static const DATE_FORMAT = 'dd/MM/yyyy';
  String formattedDate(DateTime dateTime) {
    print('dateTime ($dateTime)');
    return DateFormat(DATE_FORMAT).format(dateTime);
  }
}

我的输出变成这个

I / flutter(5209):日期是2019-11-20T00:00:00.000 + 08:00

I / flutter(5209):dateTime(2019-11-19 16:00:00.000Z)

I / flutter(5209):现在更改为19/11/2019

为什么会从20变为19?

datetime flutter dart
1个回答
0
投票

您可以使用intl package格式化任何日期,例如,

// your_string_date will be like 2019-11-20T00:00:00.000+08:00
DateTime dateTime =DateTime.parse(your_string_date_here);

您可以按照自己喜欢的方式将DateTime转换为String格式的增益,

// Your date format like "dd MMM, yyyy"
DateFormat dateFormat = DateFormat("dd/MM/yyyy");
final date= dateFormat.format(dateTime);

您完成了。

© www.soinside.com 2019 - 2024. All rights reserved.