Wednesday, March 28, 2012

Subtract days from a Date

How can I code my app to get this date:

11/1/2203

From this date:

11/15/2003

I need to Subtract 14 days from 11/15/2003 but I am not sure to go about doing this.

ThanksThis may help

Dim dteNewDate As Date
Dim dteOldDate As Date = "01/01/03"
Dim intNoOfDays As Int16 = 11

dteNewDate = DateAdd("d", -intNoOfDays, dteOldDate)

TextBox1.Text = dteNewDate
The easiest way to do this is to pass a negative number into Date.AddDays(). I was able to perform the transformation you described using the following code:


Private Sub Calendar1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calendar1.SelectionChanged
Label1.Text = Calendar1.SelectedDate.AddDays(-14)
End Sub

(This code should be run on a page with a Label and Calendar control if you want to try it out.)

Hope this helps.
<%
Dim dtmDate As DateTime
dtmDate = DateTime.Now()

Response.Write( dtmDate.ToString( "d" ) )

Dim AdddtmDate As DateTime

AdddtmDate = dtmDate.addDays(-14)

Response.write ("<br>")
Response.write (AdddtmDate)
%>
We recently wrote a simpletutorial example on working with Dates & Time in ASP.NET

Clay is right - just pass a negative number.

Bringing this back up. I found the information here very useful.

Can anyone tell me if there is a way to get what the last day of the month is? Like if the month is Febuary, I need to be able to detect 28 and 31 for March...


I don't think there is anything built in to get the last day of the month but you can use this little piece of code to get the last day of the month.

privateDateTime getLastDayOfMonth(DateTime d)

{

//get the first day of this month

int days = (d.Day - 1) * -1;

d = d.AddDays(days);

// add a month to this date

d = d.AddMonths(1);

d=d.AddDays(-1);

return d;

}

  

0 comments:

Post a Comment