views:

122

answers:

3

Is there a better way to get a static DateTime value in C# than the following?

public static DateTime StartOfRecordedHistory = DateTime.Parse("2004-01-01");

Thanks!

+5  A: 
public static DateTime StartOfRecordedHistory = new DateTime(2004, 1, 1)
eglasius
Thank you! That was quick.
JannieT
+5  A: 
public static readonly DateTime StartOfRecordedHistory = new DateTime(2004, 1, 1);

No more elegant, I admit, but it avoids any issues with locale-dependent date parsing.

If you're looking for date-time literals, unfortunately C# doesn't provide them.

itowlson
A: 

Using

public static DateTime StartOfRecordedHistory = new DateTime(2004, 1, 1)

is probably the best way as, as it's been pointed out, it doesn't have any issues with different date-time formats.

There are numerous creators for a DateTime - see the MSDN documentation, so you can have the exact format of date you want.

ChrisF