Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

DateTime issue when hour is >11

I’m working on a custom date format converter and everything seems to work fine until I get an hour > 11. Currently we’re using 24-hour format but I get the same behavior when I revert to 12 hr format using the long time converter. As as the hour reaches noon it formats 12 hrs behind.

public static string DateFormatConverter(string date)
{
    DateTime result = DateTime.ParseExact(date, "yyyyMMddhhmmss", null);
  
    string month = result.ToLongDateString().Replace(DateTime.Now.DayOfWeek + ",", "");
    month = month.Replace(DateTime.Now.Year.ToString(), "");
    month = month.Replace(DateTime.Now.Day.ToString() + ",", "");
     date = result.Day.ToString() + " " + month + result.ToString("yy") + " " + result.ToString("HH:mm:ss");
    date = date.Replace("  ", " ");
    return date;
}

Here is the input:

1)20230629105843
2)20230629124942

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the output:

1)29 June 23 10:58:43
2)29 June 23 00:49:42

>Solution :

See the docs, hh is for 12 hour format, you need HH for 24 hour format

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading