NewBie
NewBie

Reputation: 1844

Exception on DateTime Conversion

I'm trying to format a list of datetime. One date is in the format same as what i provided but the factors are not in place. That line of code is given below. Can someone tell me how to skip the error for the below line?

Convert.ToDateTime("22-01-2013 00:00:00").ToString("yyyy-MM-dd");

Upvotes: 2

Views: 8005

Answers (4)

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

Reputation: 98750

Try to use DateTime.ParseExact() method instead of.

Converts the specified string representation of a date and time to its DateTime equivalent.

public static void Main(string[] args)
{

    Console.WriteLine(DateTime.ParseExact("22-01-2013 00:00:00", "dd-MM-yyyy HH:mm:ss", CultureInfo.CurrentCulture).ToString("yyyy-MM-dd"));

}

Here is a DEMO.

Also check out Coding Best Practices Using DateTime in the .NET Framework which I think every .NET developer should read.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500145

I would avoid using Convert.ToDateTime to start with. I would suggest using DateTime.TryParse or (preferrably) DateTime.TryParseExact. Both of these will return a value indicating whether the conversion succeeded, so you don't need to start catching exceptions in order to skip bad data. For example:

DateTime parsed;
if (DateTime.TryParse(text, out parsed))
{
    string reformatted = parsed.ToString("yyyy-MM-dd");
    // Use reformatted
}
else
{
    // Log error, perhaps?
}

If you have multiple possible formats, you should consider using the overload of TryParseExact which allows you to specify multiple formats in a single call.

As well as the format, you should consider the culture you want to use. In the above code (and your code) it will use the executing thread's culture. Is that always what you want? The culture can affect all kinds of things - usually if you're specifying a custom format, you want to use the invariant culture. Otherwise you could end up using a non-Gregorian calendar unexpectedly, for example...

EDIT: If your input is always in the format dd-MM-yyyy, then you should probably use:

DateTime parsed;
if (DateTime.TryParseExact(text, "dd-MM-yyyy", CultureInfo.InvariantCulture,
                           DateTimeStyles.Default, out parsed))
{
    string reformatted = parsed.ToString(CultureInfo.InvariantCulture,
                                         "yyyy-MM-dd");
    // Use reformatted
}
else
{
    // Log error, perhaps?
}

Upvotes: 7

davioooh
davioooh

Reputation: 24676

Try with:

DateTime.ParseExact("22-01-2013 00:00:00","dd-MM-yyyy HH:mm:ss",CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");

This way you can specify the exact format for your date-string.

Upvotes: 0

Jamiec
Jamiec

Reputation: 136094

Instead of Convert.ToDateTime use DateTime.Parse or DateTime.ParseExact

ParseExact gives you more control over the format, so for example:

DateTime.ParseExact("22-01-2013 00:00:00","dd-MM-yyyy HH:mm:ss",CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");

There is also a TryParseExact variant, which allows you to gracefully handle parse errors.

Upvotes: 2

Related Questions