Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When I call this code:

DateTime timeStamp = new DateTime();
timeStamp = File.GetLastWriteTime(openFileDialog.FileName);

I get a DateTime in this format:

28.12.2020 21:17:29

But when I save it in a config:

Properties.Settings.Default.TimeStamp = timeStamp;
Properties.Settings.Default.Save();

It will be saved with slashes instead of dots and month and date swapped:

06/20/2020 11:44:28

When I want to compare them afterwards they dont match. (I know that these two excamples dont match, but the layout is completly different too). How can i fix it that they will be saved with the same layout.

question from:https://stackoverflow.com/questions/65945370/datetime-gets-saved-in-a-wrong-layout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you are comparing the value of a DateTime type object with a string (representing a DateTime), you will have to format the DateTime type value appropriately.

E.g., if you format your DateTime value as such before comparing with the string in the config, you might have success.

timeStamp.ToString("dd/MM/yyyy H:mm:ss")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...