Simple Datetime format in YYYYMMDD format

I have a little problem with the temporary format. I would like to get the result of the form YYYY-MM-DD if the datetime data is entered as 19901209 , which is 1990-12-09 .

I would also like to know if there is a way to get data in other formats, for example, DD-MM-YYYY or MM-DD-YYYY , taking into account the data generated in the form as indicated, i.e. 19901209 .

Thank you so much in advance.

+4
source share
4 answers
  string sDate = "19901209"; string format = "yyyyMMdd"; System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture; DateTime result = DateTime.ParseExact(sDate, format, provider); // result holds wanted DateTime 
+3
source
 DateTime.ParseExact("19901209", "yyyyMMdd",null).ToString("yyyy-MM-dd") 
+10
source

You can parse a date in a specific format using the DateTime.ParseExact method:

 var date = DateTime.ParseExact("19901209", "yyyyMMdd", CultureInfo.InvariantCulture); 

Then you can format it in any way using the DateTime.ToString (string) method and format string :

 date.ToString("yyyy-MM-dd"); 
+4
source

As an alternative. You can split the string "19901209" in three separate lines of the type sYear="1990" , sMonth="12" and sDate="09" using the SubString() method. Then combine these lines into one line, for example sNewDate = sYear+"-"+sMonth+"-"+sDate , and then into the DateTime dtDate variable, form the line as you need, for example DateTime dtDate = Convvert.ToDateTime(sNewDate.ToString("Your Desired Format"));

0
source

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


All Articles