Convert C# DateTime to Javascript Date

后端 未结 2 543
暗喜
暗喜 2021-02-01 01:35

I have a function in Javascript that receives a C# DateTime from MVC. If the date is null it should return \"-\", if it\'s a valid date it should return the formated date.

2条回答
  •  独厮守ぢ
    2021-02-01 02:17

    I use the following to pass a Javascript Date into C#:

    var now = new Date();
    var date = (now.getTime() / 86400000) - (now.getTimezoneOffset() / 1440) + 25569;
    

    So if you get the number of milliseconds from C#, it should be something like this:

    var csharpmilliseconds;
    var now = new Date();
    var date = new Date((csharpmilliseconds + (now.getTimezoneOffset() / 1440) - 25569) * 86400000);
    

提交回复
热议问题