Khan Abdulrehman
Khan Abdulrehman

Reputation: 854

How to convert one DateTime format to another?

I have one object {System.DateTime} value is {5/17/2010 12:00:00 AM}. I want to convert this datetime format to "d-MMM-yyyy",

string msStartDt="5/17/2010 12:00:00 AM";    
DateTime.ParseExact(msStartDt, "MM/dd/yyyy HH:mm:ss",CultureInfo.InvariantCulture).ToString("d-MMM-yyyy");

String was not recognized as a valid DateTime.

second parameter of ParseExac() method is a format specifier that defines the required format of msSartDt.

If I change {5/17/2010 12:00:00 AM} to {17-Dec-2010 12:00:00 AM} we need to change 2nd parameter of DateTime.ParseExact()

My question is how we can find programatically the format of msStartDt so we can put in second parameter of DateTime.ParseExact() method.

Upvotes: 0

Views: 3735

Answers (5)

Abdulkarim Kanaan
Abdulkarim Kanaan

Reputation: 1763

Firstly, convert your specified date from String to DateTime, then convert to another date format:

string dateString;
CultureInfo provider = CultureInfo.InvariantCulture;
dateString = "05/17/2010 12:00:00 AM";
DateTime dt = Convert.ToDateTime(dateString, provider);
Console.WriteLine(dt.ToString("d-MMM-yyyy HH:mm:ss tt"));

See other date time formats in the following link: http://www.csharp-examples.net/string-format-datetime/

Your output should be like the follow screenshot: enter image description here

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

You need to use

string msStartDt = "5/17/2010 12:00:00 AM";    
var str = DateTime.ParseExact(msStartDt, "M/dd/yyyy hh:mm:ss tt", 
                              CultureInfo.InvariantCulture).ToString("d-MMM-yyyy");

On the other hand, your question is vauge. You said I have one object DateTime value is 5/17/2010 12:00:00 AM but you have a string in your code as it.

If you have already a DateTime, you just need to format it with ToString method. You don't need parsing at all.

DateTime dt = ...
var str = dt.ToString("d-MMM-yyyy", CultureInfo.InvariantCulture);

My question is how we can find programatically the format of msStartDt so we can put in second parameter of DateTime.ParseExact() method.

It is not possible.

Think about you have a string like 01/02/2015. What is the proper format of this string? It is 1st February or 2nd January? Is it dd/MM/yyyy or MM/dd/yyyy? It is totally ambiguous as you can see. If you have a string formatted, you have to know it's proper format to parse a DateTime.

Upvotes: 2

user1672994
user1672994

Reputation: 10849

You should specify the correct parse format

DateTime.ParseExact(msStartDt, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("d-MMM-yyyy")

Upvotes: 0

Christos
Christos

Reputation: 53958

Please try this one:

DateTime.ParseExact(msStartDt, "M/dd/yyyy hh:mm:ss tt",CultureInfo.InvariantCulture);

When we use the ParseExact the format of the string we parse must be exactly the same with the string. In your case you had omitted the AM/PM designator. Furthermore you need to correct the months and hours. For futher information, please have a look here.

Upvotes: 0

Kamil Budziewski
Kamil Budziewski

Reputation: 23087

You need to specify AM/PM in your string format:

DateTime.ParseExact(msStartDt, "M/dd/yyyy hh:mm:ss tt",CultureInfo.InvariantCulture)

also you need to change from HH to hh because HH is for 24h dates

Here you have all datetime fromat constants.

Upvotes: 0

Related Questions