tags:

views:

55

answers:

1

I am using the following method to serialize a date as a string

private const string DateFormatString = "dd.MM.yyyy HH:mm:ss";
string LastsuccessfuldownloadDateTime = DateTime.Now.AddDays(-91).ToString(DateFormatString);

Is this the safest way to ensure that the string always gets serialized in this format?

Update on one server I have this running its completely getting the fields wrong.

+7  A: 

I would use explicit invariant for serialization to avoid any unexpected gotchas. You may also want to think whether you need UTC or not; for example:

string LastsuccessfuldownloadDateTime = DateTime.UtcNow.AddDays(-91).ToString(
    DateFormatString, CultureInfo.InvariantCulture);
Marc Gravell
Agreed. Even though that particular format *should* be safe, explicitly using the `InvariantCulture` is always a good idea.
Thorarin
+1. Do yourself a favor and use UtcNow. You can the always be sure what time is actually meant and you will not rely on the system being configured the same way as it was when serializing the datetime.
ntziolis
@Marc, thanks running new test now...
JL
Guys are you suggesting I replace all usages of NOW with UtcNow in my project? And essentially never use NOW for anything, since UtcNow is always more reliable?
JL
@JL: Definitely not all. It depends a little on the context of the time you're trying to save. If you're logging actions in an audit log for example, I would definitely use UtcNow. In a calendar application, it might make more sense to use local time in certain places, etc.
Thorarin
Fixed... also worth noting - I needed to restart IIS manually for some reason, even though the DLL to bin should have picked up the changes.
JL
@Thorarin, I'll investigate this, thank you.
JL