tags:

views:

453

answers:

5

How would I get the date 180 days ago using C#?

Thanks

+2  A: 

DateTime.Now.AddDays(-180)

zincorp
+3  A: 

EDIT:

DateTime day180 = Date.Now.AddDays(-180);

It's important to put it into a separate variable otherwise the value will be lost.

Joel Etherton
Pretty sure he doesn't want to know what the date was 180 **months** ago. ;)
technophile
He wants days back, not months back.
Steven
Ugh, nice catch. Fast fingers doing stupid things.
Joel Etherton
+1  A: 

DateTime.Now.AddDays(-180)

tbreffni
+17  A: 
DateTime oneEightyAgo = DateTime.Today.AddDays(-180);
masenkablast
I would use DateTime.Today as opposed to DateTime.Now because Today is just the date with 00:00 as the time, and now is the moment in time down to the millisecond
masenkablast
Many thanks for that
Jamie
This is probably the most insightful answer I've seen yet on SO - cheers
James Kolpack
Thanks, the whole DateTime.Today and DateTime.Now is a sticking point for me, especially in code review. It's the whole concept of doing it the right way as opposed to a solution that happens to work (Programming by Coincidence - The Pragmatic Programmer)
masenkablast
+2  A: 
DateTime oneEightyAgo = DateTime.Now.ToUniversalTime().AddDays(-180); 

Its best to record UTC...

Brian Leahy
There is also DateTime.UtcNow for that.
Jerome Laban