Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing French Date

I am trying to parse a French date to a DateTime object with no luck so far. Is there a way to do that?

String foo = "mar, 20 avr 2010 09:00:00 -0500";

I've already tried parsing with a different culture and changing the culture of the thread.

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA",true);
CultureInfo culture = new CultureInfo("fr-CA",true);
DateTime.Parse(foo,culture,DateTimeStyles.AdjustToUniversal);
like image 583
Edward Avatar asked Dec 22 '22 02:12

Edward


2 Answers

You can only parse (with Parse or ParseExact) what you can create when formatting a DateTime.

The closest custom format specifier to your example input is probably something like this:

ddd, dd MMM yyyy HH':'mm':'ss zzz

Code:

CultureInfo culture = new CultureInfo("fr-CA", true);

var f = new DateTimeOffset(2010, 04, 20, 09, 00, 00, TimeSpan.FromHours(-5))
            .ToString("ddd, dd MMM yyyy HH':'mm':'ss zzz", culture);

This produces the following result:

"mar., 20 avr. 2010 09:00:00 -05:00"

As you can see, the short day and short month specifier (ddd and MMM) add a . after the name, and the time-zone specifier (zzz) inserts a :.

I believe it's not possible to trick ToString into generating the desired output, and thereby also not to parse the result with ParseExact. I guess you have to parse the string yourself using plain old string manipulation.

like image 122
dtb Avatar answered Jan 03 '23 07:01

dtb


The closest I think you're going to get is

DateTime.ParseExact("mar., 01 juin 2010 12:11:53 -04:00", "ddd, dd MMM yyyy hh:mm:ss zzz", culture); // extra period after "mar"
// or
DateTime.ParseExact("mardi, 01 juin 2010 12:12:33 -04:00", "dddd, dd MMM yyyy hh:mm:sszzz", culture ); // full day name

Why DateTime.Parse doesn't work:

The documentation for DateTime.Parse says that

The s parameter must contain the representation of a date and time in one of the formats returned by the DateTimeFormatInfo.GetAllDateTimePatterns() method of the current culture.

On my computer, using this code, I get the following formats. It looks like your pattern isn't in the list.

    CultureInfo culture = new CultureInfo("fr-CA", true);

    foreach( string dateTimePattern in culture.DateTimeFormat.GetAllDateTimePatterns())
    {
        Debug.WriteLine(dateTimePattern);
    }
  • yyyy-MM-dd
  • yy-MM-dd
  • dd-MM-yy
  • yy MM dd
  • dd/MM/yy
  • d MMMM yyyy
  • d MMM yyyy
  • d MMMM yyyy HH:mm
  • d MMMM yyyy H:mm
  • d MMMM yyyy H' h 'mm
  • d MMM yyyy HH:mm
  • d MMM yyyy H:mm
  • d MMM yyyy H' h 'mm
  • d MMMM yyyy HH:mm:ss
  • d MMMM yyyy H:mm:ss
  • d MMMM yyyy H' h 'mm
  • d MMMM yyyy H:mm
  • d MMM yyyy HH:mm:ss
  • d MMM yyyy H:mm:ss
  • d MMM yyyy H' h 'mm
  • d MMM yyyy H:mm
  • yyyy-MM-dd HH:mm
  • yyyy-MM-dd H:mm
  • yyyy-MM-dd H' h 'mm
  • yy-MM-dd HH:mm
  • yy-MM-dd H:mm
  • yy-MM-dd H' h 'mm
  • dd-MM-yy HH:mm
  • dd-MM-yy H:mm
  • dd-MM-yy H' h 'mm
  • yy MM dd HH:mm
  • yy MM dd H:mm
  • yy MM dd H' h 'mm
  • dd/MM/yy HH:mm
  • dd/MM/yy H:mm
  • dd/MM/yy H' h 'mm
  • yyyy-MM-dd HH:mm:ss
  • yyyy-MM-dd H:mm:ss
  • yyyy-MM-dd H' h 'mm
  • yyyy-MM-dd H:mm
  • yy-MM-dd HH:mm:ss
  • yy-MM-dd H:mm:ss
  • yy-MM-dd H' h 'mm
  • yy-MM-dd H:mm
  • dd-MM-yy HH:mm:ss
  • dd-MM-yy H:mm:ss
  • dd-MM-yy H' h 'mm
  • dd-MM-yy H:mm
  • yy MM dd HH:mm:ss
  • yy MM dd H:mm:ss
  • yy MM dd H' h 'mm
  • yy MM dd H:mm
  • dd/MM/yy HH:mm:ss
  • dd/MM/yy H:mm:ss
  • dd/MM/yy H' h 'mm
  • dd/MM/yy H:mm
  • d MMMM
  • d MMMM
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK
  • ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  • ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
  • yyyy'-'MM'-'dd'T'HH':'mm':'ss
  • HH:mm
  • H:mm
  • H' h 'mm
  • HH:mm:ss
  • H:mm:ss
  • H' h 'mm
  • H:mm
  • yyyy'-'MM'-'dd HH':'mm':'ss'Z'
  • d MMMM yyyy HH:mm:ss
  • d MMMM yyyy H:mm:ss
  • d MMMM yyyy H' h 'mm
  • d MMMM yyyy H:mm
  • d MMM yyyy HH:mm:ss
  • d MMM yyyy H:mm:ss
  • d MMM yyyy H' h 'mm
  • d MMM yyyy H:mm
  • MMMM, yyyy
  • MMMM, yyyy
like image 31
Greg Avatar answered Jan 03 '23 08:01

Greg