简体   繁体   English

两个日期之间的天数超出预期

[英]Unexpected number of days between two dates

I am working on Windows 7 OS and calculating the number of days between two days using the code below: 我正在Windows 7操作系统上工作,并使用以下代码计算两天之间的天数:

DateTime dt = Convert.ToDateTime(txtNewIDDtAccRej.Text);
DateTime dtt = Convert.ToDateTime(txtNewIDDtInternal.Text);

TimeSpan t = dt - dtt;
txtNewProcessTime.Text = Convert.ToInt32(t.TotalDays).ToString();

This code is working fine on my system and values are coming like this as in below image: 这段代码在我的系统上运行良好,并且值如下图所示:

此搜索

In the above image, the time part is 00:00:00. 在上图中,时间部分为00:00:00。

But the problem is after deploying the same code in test server which runs Windows Server 2003 R2, the difference between the two dates is coming wrong. 但是问题是在运行Windows Server 2003 R2的测试服务器中部署了相同的代码后,两个日期之间的差错了。 Suppose the difference between 08-02-2012 and 07-02-2012 is 1 but on the test server it is 31. I am using exactly the same code. 假设2012年8月2日与2012年7月2日之间的差异为1,但在测试服务器上为31。我使用的是完全相同的代码。

Here is image for test server: 这是测试服务器的图像:

测试服务器的图像

As per the image the time value is 12:00:00. 根据图像,时间值为12:00:00。 What may be the cause of this? 这可能是什么原因?

I think it's because of date formatting (localization). 我认为是因为日期格式(本地化)。 In your computer 08-02-2012 means February 8th ( dd-MM-yyyy ) and 07-02-2012 is February 7th hence the difference is one day. 在您的计算机08-02-2012February 8thdd-MM-yyyy )和07-02-2012February 7th ,因此不同的是一天。 On server this is interpreted as August 2nd and July 2nd ( MM-dd-yyyy ). 在服务器上,这被解释为August 2nd July 2ndJuly 2nd August 2nd July 2ndMM-dd-yyyy )。 So the difference is 31 days. 因此相差31天。
Below is the snippet to solve the problem: ( not tested ) 以下是解决问题的代码段:( 未测试

 CultureInfo en = new CultureInfo("en-US");
 var format = "dd/MM/yyyy";
 DateTime myDate =  DateTime.ParseExact(myDateStr,format,en.DateTimeFormat);

Instead of doing Convert.ToDateTime use DateTime.TryParse with CultureInfo . 代替执行Convert.ToDateTimeDateTime.TryParseCultureInfo一起使用。 The issue might be on the server Culture is different than your local machine. 问题可能出在服务器上文化与本地计算机不同。

OR 要么

You can use DateTime.ParseExact . 您可以使用DateTime.ParseExact Here you can specify the date format also. 您还可以在此处指定日期格式。 Read more @ MSDN . 阅读更多@ MSDN

You can try this 你可以试试这个

string s = "08-02-2012";
string format ="dd-MM-yyyy";
//Get DateTime
DateTime dateTime = DateTime.ParseExact(s, format,new System.Globalization.CultureInfo("en-US"));
//Get string representation of DateTime
string date = dateTime.ToString(format, new System.Globalization.CultureInfo("en-US"));

This will work same on all machines. 这将在所有计算机上相同。

Hope this solves your issue. 希望这能解决您的问题。

The two systems are reporting the same time in different formats. 这两个系统以不同的格式同时报告时间。 Your system is reporting the time in 24 hour format (00:00:00) and the test server is using 12 hour format (12:00:00 AM). 您的系统以24小时制(00:00:00)报告时间,而测试服务器使用12小时制(12:00:00 AM)。 For calculation purposes, the dates are the same. 出于计算目的,日期是相同的。 If you require a specific format on output, use DateTime.Format(). 如果在输出上需要特定的格式,请使用DateTime.Format()。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM