3

How can I convert this to date time?

var datestring = "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)"

To date format like "09/06/2011". I don't mind whether server side or it is client side.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jonathan
  • 1,659
  • 8
  • 34
  • 54

9 Answers9

7

I don't think there is any BuiltIn method provided by .Net framework to parse date in this format. You can strip the infomration for timezone from the string and then parse it like:

var datestring = "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)";
DateTime dt = DateTime.ParseExact(datestring.Substring(0, 24),
                  "ddd MMM dd yyyy HH:mm:ss",
                  CultureInfo.InvariantCulture);

Later you can format it like:

string formattedDate = dt.ToString("dd/MM/yyyy");
Habib
  • 219,104
  • 29
  • 407
  • 436
1

@Naidu, You can do this client side itself.

If you are working in kendo ui, there is an option for date formatting, otherwise you can use plain javascript

In kendoUI:

var dateString = 'Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)';

// Date Formating in Kendo
alert(kendo.toString(new Date(dateString), 'dd/MM/yyyy'));

In javascript :

//  Date Formating in Javascript
var date = new Date(dateString);
alert(date.getDate() + '/' + Number(date.getMonth() + 1) + '/' + date.getFullYear());

Both output will give result as you asked (09/06/2011). You can also get 'date with time'

Refer jsFiddle here: http://jsfiddle.net/pTcJh/1/

Muthu
  • 770
  • 5
  • 13
0

This would be an opportunity:

string day = datestring.SubString(8, 2);
string month = datestring.SubString(4, 2);
switch(month)
{
    case "Jun":
        month = "06";
        break;
}
string year = datestring.SubString(11, 4);
string date = day + "/" + month + "/" + year;
Yami
  • 1,405
  • 1
  • 11
  • 16
0

did you tried putting this string into the constructor of new Date(dateString)?

it worked for me

new Date( datestring )
ripu1581
  • 300
  • 1
  • 9
  • @Naidu what does this code new Date( datestring ) returned to you? After this you can fetch day, month and year from the date and create a new date format. – ripu1581 Apr 19 '13 at 07:52
  • I am not getting only date field in c# – Jonathan Apr 19 '13 at 07:53
  • @Naidu Yes, you will get only date object, you will have to fetch day, month and year from it, concatenate them to get the formatted date – ripu1581 Apr 19 '13 at 07:55
  • `Date date = new Date(datestring)` is it what you are saying – Jonathan Apr 19 '13 at 07:57
  • @Naidu Yes, first you need to create date object – ripu1581 Apr 19 '13 at 07:59
  • The problem with this is it will not work with the full string. Just get part of it (up to 24 characters - see my answer below for an example), and pass that to the `DateTime()` constructor instead. – Kjartan Apr 19 '13 at 08:26
  • Hold on - I read "DateTime" where it said Date here - did you actually mean the .Net type `DateTime`, or something else? – Kjartan Apr 19 '13 at 08:27
0

you could split up the string, becuase every character has a fixed position in the string

string splits[] = datestring.Split(' ');
//switch for month "Jun" = 6
string date = string.Format("{0}/{1}/{2}", splits[2], month, splits[3]);
Postback
  • 619
  • 2
  • 9
  • 27
0

Maybe this helps: http://www.codeproject.com/Articles/14743/Easy-String-to-DateTime-DateTime-to-String-and-For

Or this: Converting a String to DateTime

Community
  • 1
  • 1
bema
  • 385
  • 2
  • 8
0

That's what I have come out with (using Linqpad):

String datestring = "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)";
var d = datestring.Split(new char[] {' '}, 5).Take(4);
DateTime dt = DateTime.Parse(d.Aggregate((x, y) => x + " " + y));
dt.Dump();

wiht result:

09.06.2011 00:00:00
mkowalik
  • 133
  • 1
  • 9
0

Well, I believe there is no direct method in .NET Framework to parse this kind of string.

Here is a dirty way :)

string datestring = "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)";
string[] s1 = datestring.Split(new string[]{" "}, 6, StringSplitOptions.RemoveEmptyEntries);

string date = "";
for (int i = 0; i < 5; i++)
{
     date = date + s1[i] + " ";
}
date = date.TrimEnd();

DateTime yourdatetime = DateTime.ParseExact(date, "ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture);
Console.WriteLine(yourdatetime);

Output will be like;

6/9/2011 12:00:00 AM

Here is a DEMO.

You can check more custom date formats from Custom Date and Time Format Strings

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

Short and simple, this should do the trick:

Updated: Scratch that, you don't even need the format provider I had earlier. This seems to work fine:

var datestring = "Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)";
var date = DateTime.Parse(new string(datestring.Take(24).ToArray()));

date.ToString("dd/MM/yyyy");
Kjartan
  • 18,591
  • 15
  • 71
  • 96