简体   繁体   English

两个日期之间的日期差

[英]date difference between two dates

I want to calculate the date difference between form date and two date..Am using timespan to calculate the difference between two date if date difference is positive means it enter another process falls means it return error message. 我想计算表单日期和两个日期之间的日期差。如果时间差为正,则使用时间跨度来计算两个日期之间的差,即表示它进入另一个进程下降意味着返回错误消息。

My partial code is here.. 我的部分代码在这里。

  TimeSpan span = Convert.ToDateTime(txtenddate.Text).Subtract(Convert.ToDateTime(txtstartdate.Text));
        int formatted = span.Days;
        if (formatted < 1)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "temp", "<script language='javascript'>alert('Invalid date difference ');</script>", false);
        } 

In the above code input is end date : 30-01-2004 start date : 01-02-2002 在上面的代码中输入的是结束日期:2004年1月30日开始日期:2002年2月1日

but it returns error message : String was not recognized as a valid DateTime. 但它返回错误消息:无法将字符串识别为有效的DateTime。

please give me a solution to solve this with out changing date format... 请给我一个解决方案,以解决此问题,而无需更改日期格式...

You should be using ParseExact to get the relevant DateTime . 您应该使用ParseExact来获取相关的DateTime

TimeSpan ts = DateTime.ParseExact("30-01-2004", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
                  - DateTime.ParseExact("01-02-2002", "dd-MM-yyyy", System.Globalization.CultureInfo.InvariantCulture);

While using Convert.ToDateTime it invokes DateTime.Parse which will try the conversion corresponding to the your current culture setting which as in this case doesn't support the format of DateTime you have, so you should rely on the ParseExact whereby you know the format in which the string is expected and achieve in fetching your result. 在使用Convert.ToDateTime它将调用DateTime.Parse ,它将尝试与您当前的区域性设置相对应的转换,因为在这种情况下,该设置不支持您拥有的DateTime格式,因此您应该依靠ParseExact来了解格式期望字符串,并在其中获取结果。

You need to specify the culture for the conversion. 您需要指定转换的区域性。 By default, it should be using the default date format for your PC but this doesn't always work. 默认情况下,它应使用PC的默认日期格式,但这并不总是有效。

You should take a look at this about specifying a format provider for the Convert.ToDateTime method http://msdn.microsoft.com/en-us/library/9xk1h71t.aspx 您应该查看有关为Convert.ToDateTime方法指定格式提供程序的信息, 网址http://msdn.microsoft.com/zh-cn/library/9xk1h71t.aspx

and this about the DateTimeFormatInfo object you will need to create to handle the culture: http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.aspx 以及有关您需要创建以处理区域性的DateTimeFormatInfo对象的信息: http : //msdn.microsoft.com/zh-cn/library/system.globalization.datetimeformatinfo.aspx

You must use CultureInfo maybe the default CultureInfo different from "en-GB"; 您必须使用CultureInfo,也许是默认的CultureInfo与“ en-GB”不同;

var cult = new System.Globalization.CultureInfo("en-GB");
TimeSpan span = Convert.ToDateTime("30-01-2004", cult).Subtract(Convert.ToDateTime("01-02-2002", cult));

for days difference 天差

  public long getDaysBetweenDates(Date d1, Date d2){
return TimeUnit.MILLISECONDS.toDays(d1.getTime() - d2.getTime());
}

Date difference between days with time 日期与时间之间的时差

Date startDate = // Set start date
Date endDate   = // Set end date

long duration  = endDate.getTime() - startDate.getTime();

long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);

Your dateformat should be like this. 您的dateformat应该是这样的。 StartDate=1/2/2002 and EndDate=3/1/2004 StartDate = 1/2/2002和EndDate = 3/1/2004

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

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