开发者

retrievind date in asp.net

I need to retrieve the current date in asp.net and then compare that wit开发者_如何学JAVAh the date given by the user in textbox1.text(mm/dd/yyyy format), if date date given is greater than current date then error else add 4months2days with that date and display it in textbox2.text.

help me please, thanking you guys,

Indranil


 DateTime dateToCompare;
 if(DateTime.TryParse(textbox1.text, out dateToCompare))
 {
     DateTime current = DateTime.Now;
     TimeSpan ts = current - dateToCompare;
     if (ts.Ticks  < 0)
     {
             //display error
     }
     else
          textbox2.text = dateToCompare.AddMonths(4).AddDays(2).ToString("mm/dd/yyyy");
     } 
 }


I'm not going to write your code, but in .NET you can use ToString to specify a date format, TryParse to get a date out of a string. And AddDays, AddMonths etc to manipulate a date.

In javascript, there's no simple way to format output, but you can use getMonth etc to prompt the individual values and concatenate a string from that. You can use a combination of getDate and setDate to manipulate dates. It automatically corrects for new months, i.e. if you run myDate.setDate( myDate.getDate() + 60 ) it'll actually increment by 60 days; you won't end up with a weird date like May 74th.

Keep in mind that months in javascript are zero-based, ie January is 0, February is 1, etc.

You can create a new date in javascript by new Date(yy, mm, dd) or new Date('yy/mm/dd'), so you could string-manipulate an input and create a date from that.

To compare two dates, you can subtract one from the other, and get the difference in milliseconds.

if ( dateA - dateB < 0 ) // dateB is greater than dateA (occurrs later)

and

var diff = Math.abs(dateA - dateB) // difference in ms, no matter which date is greater


DateTime date1 = new DateTime();
if(DateTime.TryParse(textbox1.text, out date1)){
            if (date1.CompareTo(DateTime.Now) > 0)
            {
                //Error code here
            }else
            {
                textbox2.text = date1.AddMonths(4).AddDays(2);
            }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜