开发者

How to convert any type of date to dd/mm/yyyy

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


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.


DateTime.Parse("your data").ToString("dd/MM/yyyy");


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);


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();
        }
    }
}


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/


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜