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.
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.
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");
@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/
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;
did you tried putting this string into the constructor of new Date(dateString)?
it worked for me
new Date( datestring )
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]);
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
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
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");