Best way to convert JavaScript date to .NET date

前端 未结 5 875
清歌不尽
清歌不尽 2021-02-05 11:20

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

相关标签:
5条回答
  • 2021-02-05 11:26

    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);
    
    0 讨论(0)
  • 2021-02-05 11:38

    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

    0 讨论(0)
  • 2021-02-05 11:44

    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);
    
    0 讨论(0)
  • 2021-02-05 11:46

    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.

    0 讨论(0)
  • 2021-02-05 11:53

    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); 
    }
    
    0 讨论(0)
提交回复
热议问题