Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert JavaScript date to .NET date

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 the date to .NET date . Note that my client side users can be anwyehere around the world. I will have the date from there now my need is to convert it to the .NET date. can you help me ?

like image 871
Rocky Singh Avatar asked Apr 02 '11 06:04

Rocky Singh


2 Answers

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),
                              "ddd 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.

like image 113
Naraen Avatar answered Oct 06 '22 02:10

Naraen


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);
like image 28
SumairIrshad Avatar answered Oct 06 '22 01:10

SumairIrshad