tags:

views:

130

answers:

6

I receive text from a *.csv file in any date format

For example: dd/mm/yy or dd/mm/yyyy or mm/dd/yyyy or 4 may 2010......

How I can convert to just a single type of format: dd/mm/yyyy ?

I'm working on C#, .NET 3.5, WinForms

Thanks in advance

+3  A: 
DateTime.Parse("your data").ToString("dd/MM/yyyy");
John Gietzen
A: 

You simply want to be using the DateTime.ParseExact together with the DateTime.ToString methods.

The straight DateTime.Parse method has its uses of course, and can be clever for parsing dates that you know are in a specific culture/locale, but since it seems dates given to you may be in an arbitrary format that cannot be recognised, you may want to specifically use ParseExact.

Example:

var myDate = DateTime.ParseExact("07/14/2010", "MM/dd/yyyy",
    CultureInfo.CurrentCulture);

var standardDateString = myDate.ToString("dd/MM/yyyy");
Noldorin
And to make thing even easier, you can even supply multiple formats to the `ParseExact` call to handle all various formats!
marc_s
See my answer - "mm" means minutes.
Jon Skeet
Sure. You could have just edited the answer though. :) It's an easy slip up to make.
Noldorin
+9  A: 

If you're receiving data in multiple formats and you can't identify them, you've got problems. What does "09/07/2010" mean? September 7th or July 9th? This is the first thing to get straight in your mind, and it has nothing to do with technology. You have two contradictory formats - how are you going to deal with them? Sample the file and pick whichever looks most likely? Treat each line separately, favouring one format over another? Ask the user?

Once you've parsed the data correctly, formatting it in the desired way is easy, as per John's answer. Note that you must use "MM" for the month, not "mm" which represents minutes. You should also specify which culture to use (affecting the date separators) assuming you don't just want to take the system default.

Jon Skeet
+2  A: 

Check out TryParseExact.

public static string FormatDate(string input, string goalFormat, string[] formats)
{
    var c = CultureInfo.CurrentCulture;
    var s = DateTimeStyles.None;
    var result = default(DateTime);

    if (DateTime.TryParseExact(input, formats, c, s, out result))
        return result.ToString(goalFormat);

    throw new FormatException("Unhandled input format: " + input);
}

Example Usage

var formats - new[] { "dd/MM/yy", "dd/MM/yyyy" };
var next = csvReader.Get("DateField");
var formattedDate = FormatDate(next, "dd/MM/yyyy", formats);
ChaosPandion
You could supply an **array of date formats** to TryParseExact - no need to call it several times! `if (DateTime.TryParseExact(input, _formats.ToArray(), c, s, out result))` and use that in just one call.
marc_s
@marc_s - Thanks.
ChaosPandion
thank's for the help, how to use this method ? what to send ? and what i get ?
Gold
A: 
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace dateconvert
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime x = Convert.ToDateTime("02/28/10");
            Console.WriteLine(string.Format(x.ToString("d", DateTimeFormatInfo.InvariantInfo)));
            DateTime y = Convert.ToDateTime("May 25, 2010");
            Console.WriteLine(string.Format(y.ToString("d", DateTimeFormatInfo.InvariantInfo)));
            DateTime z = Convert.ToDateTime("12 May 2010");
            Console.WriteLine(string.Format(z.ToString("d", DateTimeFormatInfo.InvariantInfo)));
            Console.Read();
        }
    }
}
SomeMiscGuy
Now try it with "04/02/10" - in a UK and then a US culture. Which is the right answer for an arbitrary CSV file?
Jon Skeet
a presumption on my part that since he listed the dates in the given format that it didn't matter, but you are correct (as always) that without knowing the culture, its a problem.
SomeMiscGuy
A: 

String.Format("{0:MM/dd/yyyy}", DateTime.Now);

String.Format("{0:dd/MM/yyyy}", DateTime.Now);

etc.

Source: http://www.csharp-examples.net/string-format-datetime/

MrDAT