Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is "YYYY-MM-DD HH':'MM':'SS" DateTime Format Displaying Incorrectly?

 DateTime timeUtcWhenCommentPostingOccurred = getDateAndTimeOfCommentPostingInUtc();
 DateTime estTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtcWhenCommentPostingOccurred, estZone);


 estTime.ToString("YYYY-MM-DD HH':'MM':'SS");

The above specified format shows the following Incorrect date format :

YYYY-11-DD 21:11:SS

Why does the year, day and second fail to show up properly? Please provide suggestions as to how I can fix the issue above.

like image 567
user1338998 Avatar asked Nov 29 '22 11:11

user1338998


2 Answers

  • Because its lower case y for Year, not upper case Y.
  • Same is the case with Day, its lower case d not upper case D
  • With Minutes its lower case m not upper case M, upper case M is for month,
  • For seconds it lower case s, not upper case S
  • Also remove the single quote in your format, since you don't want to escape string literals

See:Custom Date and Time Format Strings

So your format should be:

estTime.ToString("yyyy-MM-dd HH:mm:ss");
like image 51
Habib Avatar answered Dec 05 '22 19:12

Habib


Format strings are care-sensitive. YYYY, DD, and SS are not recognized format strings for DateTime, so they are treated as literal characters.

Use

estTime.ToString("yyyy-MM-dd HH:mm:ss");

instead.

Note the distinction between MM (month) and mm (minute).

like image 24
D Stanley Avatar answered Dec 05 '22 20:12

D Stanley