Datetime 在flatter中将字符串转换为日期时间
我有这个密码Datetime 在flatter中将字符串转换为日期时间,datetime,flutter,dart,Datetime,Flutter,Dart,我有这个密码 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 cons
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/颤振(5209):日期为2019-11-20T00:00:00.000+08:00
I/颤振(5209):日期时间(2019-11-19 16:00:00.000Z)
I/颤振(5209):现在改为2019年11月19日
为什么它会从20变为19?这是因为你持有的价值观
obj.到期日
根据日志,当前值为
2019-11-20T00:00:00.000+08:00
当我使用下面的代码时
var tempDate = '2019-11-20T00:00:00.000+00:00';
print('The date is '+tempDate);
print('now change to ' +DateUtil().formattedDate(DateTime.parse(tempDate)));
日志如下:
I/flutter (18268): The date is 2019-11-20T00:00:00.000+00:00
I/flutter (18268): dateTime (2019-11-20 00:00:00.000Z)
I/flutter (18268): now change to 20/11/2019
I/flutter (18268): 2019-11-15 16:07:35.078897
I/flutter (18268): The date is 2019-11-15 05:07:35.079251Z
I/flutter (18268): dateTime (2019-11-15 05:07:35.079251Z)
I/flutter (18268): now change to 15/11/2019
这些代码之间唯一的变化是我们传递的值
2019-11-20T00:00:00.000+00:00
这纯粹是一个时区问题
试试下面的代码
var tempDate = DateTime.now().toLocal().toString();
日志显示
I/flutter (18268): 2019-11-15 16:06:54.786814
I/flutter (18268): The date is 2019-11-15 16:06:54.787186
I/flutter (18268): dateTime (2019-11-15 16:06:54.787186)
I/flutter (18268): now change to 15/11/2019
同样,当您使用以下代码时
var tempDate = DateTime.now().toUtc().toString();
日志如下:
I/flutter (18268): The date is 2019-11-20T00:00:00.000+00:00
I/flutter (18268): dateTime (2019-11-20 00:00:00.000Z)
I/flutter (18268): now change to 20/11/2019
I/flutter (18268): 2019-11-15 16:07:35.078897
I/flutter (18268): The date is 2019-11-15 05:07:35.079251Z
I/flutter (18268): dateTime (2019-11-15 05:07:35.079251Z)
I/flutter (18268): now change to 15/11/2019
因此,最终的答案是,改变线以下
DateUtil().formattedDate(DateTime.parse(tempDate)));
到
这是因为你所持有的价值观
obj.到期日
根据日志,当前值为
2019-11-20T00:00:00.000+08:00
当我使用下面的代码时
var tempDate = '2019-11-20T00:00:00.000+00:00';
print('The date is '+tempDate);
print('now change to ' +DateUtil().formattedDate(DateTime.parse(tempDate)));
日志如下:
I/flutter (18268): The date is 2019-11-20T00:00:00.000+00:00
I/flutter (18268): dateTime (2019-11-20 00:00:00.000Z)
I/flutter (18268): now change to 20/11/2019
I/flutter (18268): 2019-11-15 16:07:35.078897
I/flutter (18268): The date is 2019-11-15 05:07:35.079251Z
I/flutter (18268): dateTime (2019-11-15 05:07:35.079251Z)
I/flutter (18268): now change to 15/11/2019
这些代码之间唯一的变化是我们传递的值
2019-11-20T00:00:00.000+00:00
这纯粹是一个时区问题
试试下面的代码
var tempDate = DateTime.now().toLocal().toString();
日志显示
I/flutter (18268): 2019-11-15 16:06:54.786814
I/flutter (18268): The date is 2019-11-15 16:06:54.787186
I/flutter (18268): dateTime (2019-11-15 16:06:54.787186)
I/flutter (18268): now change to 15/11/2019
同样,当您使用以下代码时
var tempDate = DateTime.now().toUtc().toString();
日志如下:
I/flutter (18268): The date is 2019-11-20T00:00:00.000+00:00
I/flutter (18268): dateTime (2019-11-20 00:00:00.000Z)
I/flutter (18268): now change to 20/11/2019
I/flutter (18268): 2019-11-15 16:07:35.078897
I/flutter (18268): The date is 2019-11-15 05:07:35.079251Z
I/flutter (18268): dateTime (2019-11-15 05:07:35.079251Z)
I/flutter (18268): now change to 15/11/2019
因此,最终的答案是,改变线以下
DateUtil().formattedDate(DateTime.parse(tempDate)));
到