Wednesday, March 28, 2012

Subtracting from a Date

Hello,

In .NET code, I need to subtract a month from the current date; is there something in the framework that does this; there is some care that is needed to do this, especially with 28-day and 30-day months, so I was wondering if there was something built-in.

Thanks.

If I understand you question right you could do this:

DateTime dtTemp = DateTime.Now;

int Month = dtTemp.Month;

Cheers

Al


Dim dAs DateTime

d = DateTime.Now

d = d.AddMonths(-1)


If you write VB.NET try the link below for DateDiff function a clone of the T-SQL version. Hope this helps.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctdatediff.asp


Hello,

Try it:

Dim LastMonth As Date = DateAdd(DateInterval.Month, -1, #3/27/2006#)

You'll found this function under following namespace:
Microsoft.VisualBasic

An this will solve your problem related to 28-days/30-days. Functions takes care of this.

Regards

Kuldeep Deokule

0 comments:

Post a Comment