如何获得日期差 [英] How to get Diffrences of dates
问题描述
大家早上好,
我正在制作一个网页,因为我使用Ajax日历来选择两个日期,例如TO date和From date,而且我还有一个总天数的文本框.
因此,当用户选择日期和起始日期时,这些日期之间的差异将显示在文本框中.那么如何找到这些日期的Diffenrece ?.
我将格式设置为dd/MM/yyyy.
例如一个文本框有:2012年4月4日
第二个:2012年2月5日.
因此,请找到差异.在这些上吗?
在此先感谢....
Mitesh
Good Morning To all,
I am making a web page in that i use Ajax calendar to pick two date like TO date and From date and i have also one Textbox of total days.
so when user select to and from date, the difference of these dates in display in Textbox. so how can i find Diffenrece of These dates ?.
I set the Formate like dd/MM/yyyy.
e.g. one textbox have : 20/04/2012
second have : 02/05/2012.
so, please find diff. on these ?
Thanks in Advance....
Mitesh
推荐答案
您可以使用JavaScript,方法如下:
You can do it using JavaScript as:
<script type="text/javascript">
t1="01/11/2011" ;
t2="28/11/2011";
var one_day=1000*60*60*24;
var x=t1.split("/");
var y=t2.split("/");
var date1=new Date(x[2],(x[1]-1),x[0]);
var date2=new Date(y[2],(y[1]-1),y[0]);
var month1=x[1]-1;
var month2=y[1]-1;
_Diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day));
1.System.DateTime dtTodayNoon = new System.DateTime(2006, 9, 13, 12, 0, 0);
System.DateTime dtTodayMidnight = new System.DateTime(2006, 9, 13, 0, 0, 0);
System.TimeSpan diffResult = dtTodayNoon.Subtract(dtYestMidnight);
2.dateDiff= (date1 - date2)
textBox1.Text =(To_datetime-From_datetime).TotalDays. ToString();
textBox1.Text = (To_datetime - From_datetime).TotalDays.ToString();
这篇关于如何获得日期差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!