DateTime to javascript date

前端 未结 10 1459
忘掉有多难
忘掉有多难 2020-11-28 05:40

From another answer on Stackoverflow is a conversion from Javascript date to .net DateTime:

long msSinceEpoch = 1260402952906; // Value from Date.getTime() i         


        
10条回答
  •  攒了一身酷
    2020-11-28 05:46

    If you use MVC with razor

    -----Razor/C#

    var dt1 = DateTime.Now.AddDays(14).Date;
        var dt2 = DateTime.Now.AddDays(18).Date;
    
        var lstDateTime = new List();
        lstDateTime.Add(dt1);
        lstDateTime.Add(dt2);
    

    ---Javascript

    $(function() {
    
                var arr = []; //javascript array
    
                @foreach (var item in lstDateTime)
                 {
                    @:arr1.push(new Date(@item.Year, @(item.Month - 1), @item.Day));
                 }
    
    • 1: create the list in C# and fill it
    • 2: Create an array in javascript
    • 3: Use razor to iterate the list
    • 4: Use @: to switch back to js and @ to switch to C#
    • 5: The -1 in the month to correct the month number in js.

    Good luck

提交回复
热议问题