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

Parse Particular Date Format C#

Can anybody help me convert this into date time format?

20210115122710

I need in format like

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

  DateTime date = DateTime.ParseExact("20210115122710", "dd/MM/yyyy hh:mm:ss", null);

>Solution :

You are parsing DateTime with wrong format, use "yyyyMMddHHmmss" instead of "dd/MM/yyyy hh:mm:ss",

DateTime date = DateTime.ParseExact("20210115122710", "yyyyMMddHHmmss", null);

Now whenever you want to print it in given format, then use .ToString() method,

string dateInGivenFormat = date.ToString("dd/MM/yyyy HH:mm:ss");

Note: I used HH for hours instead of hh.

  • HH : Use of HH converts hours in 24-hours format i.e from 00 to 23.

  • hh : hh converts hours in 12-hours format i.e from 00 to 12.

If your time is in 12-hour format then use hh otherwise use HH.

Try it online

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