get date

M

Mike

If I have a date such as 4/2/2008, is there a way to go back a month such as
3/2/2008?
 
H

Hans Kesting

Mike explained :
If I have a date such as 4/2/2008, is there a way to go back a month such as
3/2/2008?

DateTime dt = new DateTime(2008,4,2);
dt = dt.AddMonth(-1);


Hans Kesting
 
M

Mike

I've tried it and it gives me this error when I try to compile it:


DateTime dt = new DateTime(2008,4,2);
dt = dt.AddMonth(-1);

Error 24 'System.DateTime' does not contain a definition for 'AddMonth'
 
M

Mike

here is what I need.
I need to get todays date, then subtract the month and then pass only the
month to my database.
I have this;

DateTime dtToday = System.DateTime.Now;
//which gives me 4/2/2008

I then need to create a date of 3/2/2008 and only pass that month.
So if its 4/20/2008 I need to get 3/20/2008 and pass the 3.

If I do this
DateTime dt = DateTime.Parse(dtToday);
dt = dt.AddMonths(-1);

I'm getting the following error:
Error 24 The best overloaded method match for
'System.DateTime.Parse(string)' has some invalid arguments

and

Error 25 Argument '1': cannot convert from 'System.DateTime' to 'string'
 
H

Hans Kesting

Mike brought next idea :
here is what I need.
I need to get todays date, then subtract the month and then pass only the
month to my database.
I have this;

DateTime dtToday = System.DateTime.Now;
//which gives me 4/2/2008

I then need to create a date of 3/2/2008 and only pass that month.
So if its 4/20/2008 I need to get 3/20/2008 and pass the 3.

If I do this
DateTime dt = DateTime.Parse(dtToday);
dt = dt.AddMonths(-1);

I'm getting the following error:
Error 24 The best overloaded method match for 'System.DateTime.Parse(string)'
has some invalid arguments

and

Error 25 Argument '1': cannot convert from 'System.DateTime' to 'string'

You supplied a "DateTime" agrument to Parse, where it expected a
string.
Just use
DateTime dtToday = System.DateTime.Now;
DateTime dtLastMonth = dtToday.AddMonths(-1);

Hans Kesting
(sorry for the typo in my previous post)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top