views:

40

answers:

3

Hello frineds,

I need to retrieve the current date in asp.net and then compare that with 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

A: 

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
David Hedlund
The difference is in ticks, not milliseconds, because subtracting a date from another date returns a TimeSpan. You would need to use the TotalMilliseconds property of the TimeSpan to find the difference in ms
Daniel Dyson
@Daniel Dyson: those last remarks were about js, not .net
David Hedlund
Ah yes, so they were. I was wondering how someone with your reputation score could make that mistake. :)
Daniel Dyson
A: 
 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");
     } 
 }
Daniel Dyson
A: 
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);
            }
}
derek