开发者

C# DateTime, is this method regional setting safe?

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 stri开发者_运维技巧ng always gets serialized in this format?

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


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);


I think you should change the culture of your programme according to your regional setting.Then every thing on different regional setting computer will work fine.

for example if your regional setting is according to U.S and you want to run your programme on machine with uk setting .

Then change the CurrentCulture of your programme to U.S.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜