如何找出由ASP压延机选择的两个日期之间的天数 [英] how to findout days between two dates selected by asp calender

查看:74
本文介绍了如何找出由ASP压延机选择的两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须压光
和两个文本框

一个来自:textbox1和calender1
第二个是:textbox2和calender2

当我在calender1中选择一个日期..时,它在textbox1中显示选定的日期
当我在calender2中选择一个日期..时,我在textbox2中显示了选择的日期

现在我贴了标签.我希望两个"FROM"和"TO"文本框之间的日期显示在LABEl

例如:
当我选择FROM date:23/3/2012
和截止日期:2012年2月4日

标签显示了我在LABEL中的天数.在这种情况下..将是"7天"

我尝试使用此代码执行此任务


i have to calenders
and two textboxes

one is from : textbox1 and calender1
2nd is to : textbox2 and calender2

when i select a date .. in calender1 it shows slectd date in textbox1
and when i select a date .. in calender2 it shows me selectd date in textbox2

Now i placed a label . in which i want that the days between the two ''FROM'' and ''TO'' TEXTBOXES will b shown in in LABEl

FOR EXAMPLE:
when i selectd FROM date : 23/3/2012
and TO date : 2/4/2012

the label show me the number of days in LABEL . in this case.. which will be "7 DAYS"

i tries to do this task with this code


protected void Button1_Click(object sender, EventArgs e) 
       { 
           DateTime dt1 = Calendar1.SelectedDate; 
           DateTime dt2 = Calendar2.SelectedDate; 
           TimeSpan ts = dt2 - dt1; 
           int numberOfDays = ts.Days; 
           Label3.Text = numberOfDays.ToString(); 
       } 



但找不到我想要的结果,



but not found my desired result,

推荐答案

我认为使用的方法很好.
关于从stringDate 的转换,如果使用了Parse方法,则
格式设置受当前DateTimeFormatInfo对象的属性影响,默认情况下,这些属性是从控制面板"中的区域和语言选项"项派生的.解析方法会意外引发FormatException的原因之一是,如果当前的DateTimeFormatInfo.DateSeparator和DateTimeFormatInfo.TimeSeparator属性设置为相同的值.如此处所述
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx [ ^ ]

因此,如果在TextBox 中显示的日期Format 是固定的,我们说dd/MM/yyyy,则ParseExact 方法在这里说明
I think the method used is good.
Regarding Conversion of Date from string, if the Parse method is used then
Formatting is influenced by properties of the current DateTimeFormatInfo object, which by default are derived from the Regional and Language Options item in Control Panel. One reason the Parse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator properties are set to the same value. as explained here
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx[^]

Hence, if the Format of date being displayed in the TextBox is fixed, let us say dd/MM/yyyy, then the ParseExact method explained here http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^] where CultureInfo.InvariantCulture is recommended in the Note for custom format, may be better as shown below
DateTime dt2 = DateTime.ParseExact("02/04/2012","dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);


进一步这两个陈述


Further these two statements

int numberOfDays = ts.Days;
Label1.Text = numberOfDays.ToString();


可以组合为


can be combined as

Label1.Text = ts.Days.ToString();


如果numberOfDays没有其他用途.


if there is no other purpose for numberOfDays.


朋友...

您也可以尝试

date1 ="23/03/1989"
date2 ="28/04/2012"
Hi Friend...

U can also try this

date1 = "23/03/1989"
date2 = "28/04/2012"
ts = date2.Subtract(date1)  

iNumberOfDays = ts.Days  

strMsgText = "The total number of days elapsed since " _  
& date1.ToShortDateString() & " is: " & iNumberOfDays.ToString()


这篇关于如何找出由ASP压延机选择的两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆