5

I'm using http://www.eyecon.ro/bootstrap-datepicker/ plugin to select date, and after date is selected I get e.g. Fri Nov 01 2013 00:00:00 GMT+0100

1) Why am I getting that date format if I set up the plugin with format yyyy-mm-dd ?

2) How to parse Fri Nov 01 2013 00:00:00 GMT+0100 to DataTime with format yyyy-mm-dd ?

Tony
  • 12,405
  • 36
  • 126
  • 226
  • I assume that the datepicker returns a `Date` object. Wouldn't it be more convenient to pass it as unix time? `new Date().getTime()/1000`. http://stackoverflow.com/questions/1674215/parsing-unix-time-in-c-sharp – Johan Nov 09 '13 at 17:31

2 Answers2

11

You can use "ddd MMM dd yyyy HH:mm:ss 'GMT'K" format with DateTime.ParseExact like;

string s = "Fri Nov 01 2013 00:00:00 GMT+0100";
DateTime dt = DateTime.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K",
                       CultureInfo.InvariantCulture);
Console.WriteLine(dt);

Output will be;

10/31/2013 11:00:00 PM

Here a demonstration.

For more informations, take a look at;

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • it works - but what can you tell about the 1) question ? does that provided date format string depends on user's browser language/OS language. I'm trying figure out that because I want only one date format to be sent to the server – Tony Nov 09 '13 at 19:13
0
    public string GmtDateTimeString { get; set; }
     
    public static readonly string[] DateFormats =
    {
                "MM/dd/yyyy hh:mm:ss tt",
                "MM/dd/yyyy HH:mm:ss"
    };

    public bool TryParseUtcDate(out DateTime d)
    {
                if (DateTime.TryParseExact(GmtDateTimeString, DateFormats,
                    CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d))
                {
                    return true;
                }
    
                if (DateTime.TryParse(GmtDateTimeString, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d))
                {
                    return true;
                }
    
                d = DateTime.MinValue;
    
                return false;
   }
Abdullah Tahan
  • 1,963
  • 17
  • 28