Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# - Parse dates

Tags:

.net

datetime

f#

Very basic question regarding parsing date in F#. I am very new to F# and .NET, so please bear with me.

My date has the format yyyyMMDD like 20100503.

How do I parse this into a DateTime type in F#.

I tried System.DateTime.Parse("20100503"); but get an error.

How do I pass a format string into the Parse method??

ANSWER is - Thanks everyone for the responses.

let d = System.DateTime.ParseExact("20100503", "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
like image 790
Raghs Avatar asked Nov 11 '10 18:11

Raghs


2 Answers

You should be able to use a custom format string. Try this:

System.DateTime.ParseExact("20100503", "yyyymmdd", null);;

See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for more details.

like image 57
Steve Avatar answered Sep 18 '22 10:09

Steve


DateTime.ParseExact("20100503", "yyyyMMdd", CultureInfo.CurrentCulture)

Use the ParseExact method and use lowercase d instead of upper.

like image 41
Greg Avatar answered Sep 18 '22 10:09

Greg