2

Possible Duplicate:
Date time format from string?

Does anyone know how I could convert the following string to a DateTime value in C# ?

"Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)"
Community
  • 1
  • 1
  • Also look http://stackoverflow.com/questions/919244/converting-string-to-datetime-c-net – Berkay Turancı Dec 15 '12 at 10:14
  • @Bhushan Firake. i have tried DateTime.ParseExact(string,format,format provider) but what format should i use for the above –  Dec 15 '12 at 10:22
  • 1
    @lafama: You can go through this article http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For – Bhushan Firake Dec 15 '12 at 10:24

4 Answers4

6

If you only have strings ending with "GMT+0300 (E. Africa Standard Time)", you can try:

string dateString = "Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)";
DateTime date = DateTime.ParseExact(dateString, "ddd MMM dd yyyy HH:mm:ss 'GMT+0300 (E. Africa Standard Time)'", System.Globalization.CultureInfo.InvariantCulture);

The meanings of the specifiers are as follows:

  • "ddd" The abbreviated name of the day of the week.
  • "MMM" The abbreviated name of the month.
  • "dd" The day of the month, from 01 through 31.
  • "yyyy" The year as a four-digit number.
  • "HH" The hour, using a 24-hour clock from 00 to 23.
  • "mm" The minute, from 00 through 59.
  • "ss" The second, from 00 through 59.
  • ":" The time separator.
  • "string", 'string' Literal string delimiter.

You can find out more about different format specifiers in the MSDN article named Custom Date and Time Format Strings

Moreover, if you want to parse "GMT+0300 (E. Africa Standard Time)" part too, I think you should implement a way to parse them yourself. I don't think there's a specifier for that.

hattenn
  • 4,371
  • 9
  • 41
  • 80
  • 1
    In principle your list of specifiers should also mention: • `":"` The `TimeSeparator` of the format provider you use; will translate to `:` for the invariant culture. Translates to `.` for some other cultures. – Jeppe Stig Nielsen Dec 15 '12 at 12:09
  • @Jeppe: You're right, I forgot that one. Adding it right now. – hattenn Dec 15 '12 at 19:33
1

First of all, you should Africa Standart Time culture info use for yours';

CultureInfo( "af-ZA", false );

But your string is really complex for converting to DateTime. For me it looks imposible to convert to DateTime perfectly. But we can some rehabilitation in your string. For example, if your string was like this; "11/15/2012 00:00:00" you can convert it like this;

using System;
using System.Globalization;

namespace Programs
{
    public class Program
    {      
        public static void Main(string[] args)
        {
            string str = "11/15/2012 00:00:00";
            DateTime dt = DateTime.ParseExact(str, "MM/dd/yyyy hh:mm:ss", new CultureInfo("af-ZA"));
            Console.WriteLine(dt.ToString());
        }
    }
}

Custom Date and Time Format Strings

DateTime.ParseExact Method

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Try this:

DateTime date = DateTime.Parse(yourDateTimeString);
Yustme
  • 6,125
  • 22
  • 75
  • 104
0

There is no way to handle (E. Africa Standard Time).

Assuming that UTC=GMT you can also get the time zone part, just remove not important parts of your string

string t = Regex.Replace("Thu Nov 15 2012 00:00:00 GMT+0300 (E. Africa Standard Time)", "([(].+?[)])", "");
t= t.Replace("GMT", "").Trim();

DateTime a = DateTime.ParseExact(t, "ddd MMM dd yyyy HH:mm:ss zzzz", System.Globalization.CultureInfo.InvariantCulture);
VladL
  • 12,769
  • 10
  • 63
  • 83