...

View Full Version : How do I get a dattime format in C# that will look like this:



complete
09-22-2011, 08:49 AM
How do I get a dattime format in C# that will look like this:

2011-09-01 00:00:00:000
This works
DateTime saveNow = DateTime.Now;

string test = String.Format("{0:yyyy-MM-01 00:00:00:000}", saveNow);

Now I want to know this. How would I subtract one month from the current month?

It T-SQL it would look like this
http://i67.photobucket.com/albums/h292/Athono/THIS-1.png
How would it be done in C#?


trying the addmonth method with a negative one did not work
http://i67.photobucket.com/albums/h292/Athono/thisdidnotwork.png

oracleguy
09-22-2011, 07:11 PM
The reason the call to AddMonths didn't work is that the function doesn't change the instance of DateTime you call it on. Instead it returns a new DateTime instead with the result.

So if you didn't care about saving the value of saveNow, you'd just do:

saveNow = saveNow.AddMonths(-1);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum