简体   繁体   中英

Convert date string to DateTime format

I have the date string like 03/10/1999 where the format is dd/MM/yyyy (pt-BR format).

And I need to convert this date for a SQL-like format yyyy-MM-dd HH:mm:ss.fff .

I tried to use Parse and ParseExact functions, but no success so far. I will let my results below...


Using Parse

var BrazilianDate = "03/10/1999";
var Parse = DateTime.Parse(BrazilianDate, new CultureInfo("pt-BR"));
Console.WriteLine("Parsed date: " + Parse);

Output: Parsed date: 10/3/1999 12:00:00 AM

No hyphens or milliseconds...


Using ParseExact

var BrazilianDate = "03/10/1999";
var ParseExact = DateTime.ParseExact(BrazilianDate, "yyyy-MM-dd HH:mm:ss.fff", new CultureInfo("pt-BR"));
Console.WriteLine(ParseExact);

output:

Run-time exception (line -1): String was not recognized as a valid DateTime.

Stack Trace:

[System.FormatException: String was not recognized as a valid DateTime.] at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style) at System.DateTime.ParseExact(String s, String format, IFormatProvider provider) at Program.Main()

You need to format your output with the correct format string like this:

Console.WriteLine("Parsed date: " + Parse.ToString("yyyy-MM-dd HH:mm:ss.fff"));
//Parsed date: 1999-10-03 00:00:00.000

If you don't specify a format, .NET picks whatever it thinks is the right one (which it often isn't when you're not in the US).

You also need to strictly separate between the DateTime value and its representation in string form. No matter how you format it, the value itself will stay the same.

The format string you use in the parse method represents the format of the input string.

A DateTime does not have a display format, in fact it's a numeric value representing the number of ticks since a specific Epoch. From official documentation :

Time values are measured in 100-nanosecond units called ticks. A particular date is the number of ticks since 12:00 midnight, January 1, 0001 AD (CE) in the GregorianCalendar calendar. The number excludes ticks that would be added by leap seconds. For example, a ticks value of 31241376000000000L represents the date Friday, January 01, 0100 12:00:00 midnight.

When parsing strings, I find it's best to either use ParseExact or TryParseExact . To print our the string representation of the DateTime value, use the overload of ToString that takes in a string that represent the format you want to display.

var BrazilianDateString = "03/10/1999";
var DateTimeValue = DateTime.ParseExact(BrazilianDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(DateTimeValue.ToString("yyyy-MM-dd HH:mm:ss.fff");

This code is working for me:

        DateTime dt = new DateTime();

        string x = "03/10/1999 22:10:10";

        dt = DateTime.Parse(x);

        Console.WriteLine(dt.ToShortDateString());
        Console.WriteLine(dt.ToShortTimeString());

        Console.ReadLine();

Console output:

03/10/1999

22:10

Don't use that CultureInfo, DateTime can understand spanish-brazilian dates on its own

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM