get date

  • Thread starter Thread starter Mike
  • Start date Start date
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
 
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'
 
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'
 
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)
 
Back
Top