How to convert time format?

My windows date format is month / date / year. If I want to set StartTime with the format "yyyy / MM / dd HH: mm: ss", how can I do this. I am trying the following code.

DateTime StartTime = DateTime.ParseExact("2011/01/04 09:30:00", "yyyy/MM/dd HH:mm:ss", null);

But StartTime comes out from 1/4/2011 9:30:00. (month / date / year hh: mm: ss)

+3
source share
4 answers

Your format is used to handle the date correctly, but it is displayed by default. To display it in the format in which you created it, you will need to either use String.Format , or even use "ToString ()" using the template .

+3
source

, . StartTime.ToString("yyyy/MM/dd HH:mm:ss")

+11

This code works correctly, and now you have a DateTime object with a strong type.

If you want to output it in the above format, you call ToString () with the format in the second argument.

+4
source

MSDN is your friend: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

You can also set a breakpoint to check the StartTime value to find out what is actually in it.

+2
source

Source: https://habr.com/ru/post/1783422/


All Articles