简体   繁体   English

试图减去 DateTime “System.FormatException: String '12/9/2019 12:00:00 AM' 未被识别为有效的 DateTime。”

[英]Trying to subtract DateTime "System.FormatException: String '12/9/2019 12:00:00 AM' was not recognized as a valid DateTime."

So I'm trying to make a check for an Order .所以我正在尝试检查Order The difference between ArriveTime and OrderDate should be between 10 minutes and 2 hours. ArriveTimeOrderDate之间的差异应该在 10 分钟到 2 小时之间。 I formatted everything to what I think was correct, but I still get the error.我将所有内容格式化为我认为正确的格式,但仍然出现错误。

The values I put in ArriveTime is DateTime.Today and in OrderDate I put in DateTime.Now .我在ArriveTimeArriveTime的值是DateTime.Today ,在OrderDate我输入了DateTime.Now

Code:代码:

private bool TimerRequirements(IOrderData orderData)
{
     DateTime arriveTime = DateTime.ParseExact(orderData.ArriveTime.ToString(), "dd-MM-yyyy hh:mm:ss:tt", CultureInfo.InvariantCulture);
     DateTime orderDate = DateTime.ParseExact(orderData.OrderDate.ToString(), "dd-MM-yyyy hh:mm:ss:tt", CultureInfo.InvariantCulture);


     if (orderData.ArriveTime != null && Convert.ToDateTime(arriveTime.Subtract(orderDate)) >= orderData.OrderDate.AddMinutes(10) ||
            orderData.ArriveTime != null && Convert.ToDateTime(arriveTime.Subtract(orderDate)) <= orderData.OrderDate.AddHours(2))
     {
         return true;
     }
     else
     {
         return false;
     }
}

Dates have no format, they are binary values.日期没有格式,它们是二进制值。 The code causes the error by trying to convert the dates ti strings then back to dates.该代码通过尝试将日期 ti 字符串转换回日期来导致错误。 Just use只需使用

TimeSpan diff=orderData.ArriveTime - orderData.OrderDate;

to get the difference as a TimeSpan , for example :将差异作为TimeSpan ,例如:

var diff=orderData.ArriveTime - orderData.OrderDate;
var inRange= (diff>=TimeSpan.FromMinutes(10) && diff<TimeSpan.FromHours(2));
return inRange;

The difference between two dates is a duration, not a date.两个日期之间的差异是持续时间,而不是日期。 In .NET that duration is represented by the TimeSpan class.在 .NET 中,持续时间由TimeSpan类表示。 You can generate TimeSpan values using its constructor, or through methods like FromHours and FromMinutes您可以使用其构造函数或通过FromHoursFromMinutes等方法生成 TimeSpan 值

If OrderDate and ArriveTime are DateTime?如果OrderDateArriveTimeDateTime? , you'll get a TimeSpan? ,你会得到一个TimeSpan? instead of TimeSpan .而不是TimeSpan If any of those values is null, the difference will also be null.如果这些值中的任何一个为空,则差异也将为空。

In this example :在这个例子中:

class Order{
    public DateTime? OrderDate{get;set;}
    public DateTime? ArriveTime{get;set;}
}

var orderData=new Order{};
TimeSpan? diff=orderData.ArriveTime - orderData.OrderDate;

diff is null . diffnull Both comparisons will return false so inRange will be false两个比较都将返回false因此inRange将为false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串“12/20/2022 12:00:00 AM”未被识别为有效的日期时间 - String '12/20/2022 12:00:00 AM' was not recognized as a valid DateTime 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? 无法将system.formatexception字符串识别为有效的日期时间 - system.formatexception string was not recognized as a valid datetime 如何解决此错误:System.FormatException:&#39;字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.' ExceptionType:“System.FormatException” 消息:“30/6/2019 不是 DateTime 的有效值。” - ExceptionType: "System.FormatException" Message: "30/6/2019 is not a valid value for DateTime." System.FormatException:字符串未被识别为有效的日期时间 - 尝试转换 MM/DD/YYYY 时 - System.FormatException: String was not recognized as a valid DateTime - when trying to convert MM/DD/YYYY 无法将这种格式的字符串“1/29/2020 12:00:00 AM”解析为有效的 DateTime - unable to parse a string of this format "1/29/2020 12:00:00 AM" into a valid DateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM