Parse Particular Date Format C#

Can anybody help me convert this into date time format?

20210115122710

I need in format like

  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

Leave a Reply