Date Sync Time in C #, getting the wrong output

I am trying to parse a date using below code

DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/mm/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat); 

but its output is incorrect, datetoconvert in the code above 30/Mar/2017 , but output 29/Jan/2017

I look forward to your valuable answers ...

+5
source share
3 answers

Lowercase mm means minute, use mm

 DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat); 

If you want to print it as 30/Mar/2017 (another section):

 string result = mydate.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture); 

But note that / has special meaning (in Parse and ToString ). It will be replaced by your current cultural date separator, which seems to / but not with another. You can avoid this by specifying CultureInfo.InvariantCulture or disguising it by wrapping it with apostrophes:

 DateTime mydate = DateTime.ParseExact(datetoconvert,"dd'/'MM'/'yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat); 
+13
source

replace

 "dd/mm/yyyy" 

with

 "dd/MMM/yyyy" 

since "Jan" corresponds to MMM instead of mm (in minutes)

Link

"MMM" The abbreviated name of the month.

https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

+7
source

Date format is incorrect. try "dd / mm / yyyy" instead of "dd / mm / yyyy"

If you need the name of the abbreviated month, use "dd / MMM / yyyy"

+1
source

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


All Articles