I have a date in JavaScript and its value is coming like this
Fri Apr 01 2011 05:00:00 GMT+0530 (India Standard Time) {}
Now what is the best way to convert th
Expanding on @Naraen's answer, my javascript date was in the following format:
Thu Jun 01 2017 04:00:00 GMT-0400 (Eastern Standard Time)
Which required two lower case d's for the day (dd
) for the conversion to work for me in C#. See update to @Naraen's code:
DateTime.ParseExact(dateString.Substring(0,24),
"ffffd MMM dd yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
You can convert your time to string before you send it and in the .net you should convert a string into datetime using one of datetime constructor. Datetime .net -> http://msdn.microsoft.com/en-us/library/system.datetime(v=VS.90).aspx You can use also a DateTime.Parse method -> http://msdn.microsoft.com/en-us/library/ms973825.aspx But you should deliver a correct form of string to server
Convert JavaScript into UTCString from Client side:
var testDate = new Date().toUTCString();
Parse it from C# code (you can fetch js date through webservice call).
DateTime date = DateTime.Parse(testDate);
Possible duplicate of the question answered here: Javascript date to C# via Ajax
If you want local time, like you are showing in your question the following would do it.
DateTime.ParseExact(dateString.Substring(0,24),
"ffffd MMM d yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
If you are looking for GMT time, doing a dateObject.toUTCString()
in Javascript in the browser before you send it to the server, would do it.
Ok here try this simple function which will convert your `double' representation of your Unix Timestamp
public static DateTime ConvertFromUnixTimestamp(double timestamp)
{
DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddMilliseconds(timestamp);
}