Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 从Flatter中的DateTime类获取意外日期_Flutter_Dart - Fatal编程技术网

Flutter 从Flatter中的DateTime类获取意外日期

Flutter 从Flatter中的DateTime类获取意外日期,flutter,dart,Flutter,Dart,我正在尝试合并日期和时间到日期时间格式的字符串,以便获得两个时间戳的差异 但是,当将日期作为“2019年7月31日”和时间作为“5:07PM”(以toDateTimeFormat方法中所示的正确格式)传递给DateTime构造函数时,它会给我一个意外的日期,即2019-07-01 17:07:00.000,其中预期日期应为2019-07-31 17:07:00.000 我也尝试过使用DateTime.utc构造函数,但没有成功,下面是我的代码 import 'package:intl/intl.

我正在尝试合并日期和时间到日期时间格式的字符串,以便获得两个时间戳的差异

但是,当将日期作为“2019年7月31日”和时间作为“5:07PM”(以toDateTimeFormat方法中所示的正确格式)传递给DateTime构造函数时,它会给我一个意外的日期,即2019-07-01 17:07:00.000,其中预期日期应为2019-07-31 17:07:00.000

我也尝试过使用DateTime.utc构造函数,但没有成功,下面是我的代码

import 'package:intl/intl.dart';

void main(){

  String dateOne = "31-July-2019";
  String timeOne = "5:07PM";

  String dateTwo = "01-Aug-2019";
  String timeTwo = "12:00AM";

  DateTime reminderDate = toDateTimeFormat(dateOne,timeOne);
  // 2019-07-01 17:07:00.000  which is wrong...,  EXPECTED --> 2019-07-31 17:07:00.000

  DateTime dueDate = toDateTimeFormat(dateTwo, timeTwo);

  bool value = isValidReminderDate(reminderDate, dueDate);
  // REMINDER DATE < DUE DATE SO RETURN TRUE ELSE FALSE...., EXPECTED --> TRUE
  print(value);
}

var monthsNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec"];

DateTime toDateTimeFormat(String date, String time){
  if(date != null && time != null){
    //date: 01-jan-2000 and time: "6:45PM"

    List<String> _parts = date.split("-");
    List<String> _timeParts =[];

      var dt = DateFormat("h:mma").parse(time);
      _timeParts = DateFormat('HH:mm').format(dt).split(":");

    DateTime dateTime =  DateTime(int.parse(_parts[2]),monthsNames.indexOf(_parts[1]),int.parse(_parts[0]), int.parse(_timeParts[0]), int.parse(_timeParts[1]) ,);

    // ALSO TRIED WITH DateTime.utc(int.parse(_parts[2]),monthsNames.indexOf(_parts[1]),int.parse(_parts[0]), int.parse(_timeParts[0]), int.parse(_timeParts[1]) ,);
    // but of no use...

    print("dateTime :: $dateTime");
    return dateTime;
  }
}

bool isValidReminderDate(DateTime reminderDate, DateTime dueDate){
  print('isValidReminderDate :: ${reminderDate.difference(dueDate).isNegative}');
  return reminderDate.difference(dueDate).isNegative;
}

import'包:intl/intl.dart';
void main(){
字符串dateOne=“2019年7月31日”;
字符串timeOne=“5:07PM”;
字符串dateTwo=“01-Aug-2019”;
字符串timeTwo=“12:00AM”;
DateTime提醒日期=toDateTimeFormat(dateOne,timeOne);
//2019-07-01 17:07:00.000哪个错误…,预期-->2019-07-31 17:07:00.000
DateTime dueDate=toDateTimeFormat(日期二,时间二);
布尔值=isValidReminderDate(提醒日期、决斗日期);
//提醒日期<到期日期,因此返回TRUE ELSE FALSE….,预期-->TRUE
印刷品(价值);
}
变量月名=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”];
DateTime toDateTimeFormat(字符串日期、字符串时间){
如果(日期!=null和时间!=null){
//日期:2000年1月1日,时间:下午6:45
列表部分=日期分割(“-”);
列表_timeParts=[];
var dt=DateFormat(“h:mma”).parse(时间);
_timeParts=DateFormat('HH:mm')。格式(dt)。拆分(“:”;
DateTime DateTime=DateTime(int.parse(_parts[2])、monthnames.indexOf(_parts[1])、int.parse(_parts[0])、int.parse(_timeParts[0])、int.parse(_timeParts[1]);
//还尝试了DateTime.utc(int.parse(_parts[2])、monthnames.indexOf(_parts[1])、int.parse(_parts[0])、int.parse(_timeParts[0])、int.parse(_timeParts[1]);
//但是没有用。。。
打印(“日期时间::$dateTime”);
返回日期时间;
}
}
bool isValidReminderDate(日期时间提醒日期,日期时间决斗日期){
打印('isValidReminderDate::${employerDate.difference(dueDate.isNegative}');
返回提醒日期。差异(dueDate)。为负;
}

您应该能够使用
包:intl
直接解析这些字符串(下面有一个警告)

打印
2019-07-31 17:07:00.000 2019-08-01 00:00:00.000 6:53:00.000000


需要注意的是,您必须使用
Jul
而不是
July
Sep
而不是
Sep
,因为您在其他地方使用了3个字母的缩写。

感谢您提供的解决方案,从2019-07-31 17:07:00.000起,我想排除.000部分。i、 我只想要2019-07-31 17:07:00这部分。我应该怎么做?这是由
DateTime
的naive
toString
格式化的毫秒数。要控制日期、时间和持续时间的格式,请使用上面用于分析原始字符串的同一类(
DateFormat
)。它有一个
format
方法将日期时间转换为格式化字符串。通过使用此字符串formattedDate=DateFormat('yyyy-MM-dd kk:MM:ss')。format(dateToformat)解决;谢谢你,理查德
  var dateOne = "31-Jul-2019";
  var timeOne = "5:07PM";

  var dateTwo = "01-Aug-2019";
  var timeTwo = "12:00AM";

  var format = DateFormat("dd'-'MMM'-'yyyy hh:mma");
  var one = format.parse('$dateOne $timeOne');
  var two = format.parse('$dateTwo $timeTwo');

  print('$one $two ${two.difference(one)}');