【问题标题】:Getting unexpected date from DateTime class in flutter在颤动中从 DateTime 类中获取意外日期
【发布时间】:2019-08-01 13:41:11
【问题描述】:

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

但是当将日期作为“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;
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

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

      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)}');
    

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

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

    【讨论】:

    • 感谢您的解决方案,从这个 2019-07-31 17:07:00.000 我想排除 .000 部分。即我只想要 2019-07-31 17:07:00 这部分。我该怎么做?
    • 这是由 DateTime 的天真的 toString 格式化的毫秒数。要控制日期、时间和持续时间的格式,请使用上面使用的相同类 (DateFormat) 来解析原始字符串。它有一个format 方法将日期时间转换为格式化字符串。
    • 用这个字符串解决了 formattedDate = DateFormat('yyyy-MM-dd kk:mm:ss').format(dateToformat);谢谢理查德
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    相关资源
    最近更新 更多