Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse DateTime From Odd Format [duplicate]

Tags:

c#

Possible Duplicate:
Parsing a complicated string as DateTime

I have a string date with time that I'd like to parse into a DateTime. I'm not quite sure how to tackle it because of the odd format. Help is appreciated.

Example: Mon Mar 24 13-42-30 2008

like image 490
John Avatar asked Dec 31 '25 19:12

John


2 Answers

DateTime.ParseExact should do what you want:

var dateTime = DateTime.ParseExact(
    "Mon Mar 24 13-42-30 2008",
    "ddd MMM dd HH-mm-ss yyyy",
    CultureInfo.CurrentCulture);

Can someone tell me the difference between using CultureInfo.CurrentCulture and CultureInfo.InvariantCulture, like the other answers? I was assuming things like the day name and month name might need to be parsed in their native language. Thanks.

like image 194
Jesse C. Slicer Avatar answered Jan 03 '26 10:01

Jesse C. Slicer


Try:

var theDate = DateTime.ParseExact(
    "Fri Jul 13 13-42-30 2012", 
    "ddd MMM dd HH-mm-ss yyyy", 
    System.Globalization.CultureInfo.InvariantCulture);

Date string formatting options can be found here.

like image 29
Adam Houldsworth Avatar answered Jan 03 '26 12:01

Adam Houldsworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!