Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date and time conversion in C# - DateTime.ParseExact() not working as expected

Tags:

c#

.net

datetime

I have date/time format, for example: "1-Mar-13 92230" According to this document and this link the format is as follows: "d-MMM-yy Hmmss", because:

Day is single digit, 1-30
Month is 3 letter abbreviation, Jan/Mar etc.
Year is 2 digits, eg 12/13
Hour is single digit for 24 hour clock, eg 9, 13 etc. (no 09)
Minute is standard (eg 01, 52)
Second is standard (eg 30, 02)

I'm trying to run the following code in my program, but I keep getting an error of "String was not recognized as a valid DateTime."

string input = "1-Mar-13 92330";
        var date = DateTime.ParseExact(input, "d-MMM-yy Hmmss", 
System.Globalization.CultureInfo.CurrentCulture);

Please help, I'm not too familiar with DateTime conversions, but I can't see where I've gone wrong here. Thanks!

UPDATE: Is this because time cannot be parsed without colons in between? (eg 1-Mar-13 9:22:30 gets parsed, but i have an external data source that would be impossible to rewrite from Hmmss to H:mm:ss)

like image 800
John Avatar asked Aug 22 '13 09:08

John


People also ask

How does time () function work in C?

The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.

What does Strftime mean in C?

(Write Formatted Date and Time to String) In the C Programming Language, the strftime function stores characters into the array pointed to by s based on the string pointed to by format.

What data type is time in C?

The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard <time.

What is ctime ()?

The ctime() function in C++ converts the given time since epoch to a calendar local time and then to a character representation. A call to ctime(time) is a combination of asctime() and localtime() functions, as asctime(localtime(time)) . It is defined in <ctime> header file.


2 Answers

You could fix your date:

var parts = "1-Mar-13 92230".Split(' ');

if (parts[1].Length == 5)
{
    parts[1] = "0" + parts[1];
}

var newDate = parts[0] + " " + parts[1];

var date = DateTime.ParseExact(newDate, "d-MMM-yy HHmmss",  System.Globalization.CultureInfo.CurrentCulture);
like image 50
xanatos Avatar answered Oct 04 '22 18:10

xanatos


From msdn:

If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".

so you can try:

string input = "1-Mar-13 92330";
        var date = DateTime.ParseExact(input, "d-MMM-yy Hmmss", 
System.Globalization.CultureInfo.InvariantCulture);
like image 20
Alberto Avatar answered Oct 04 '22 18:10

Alberto