Search code examples
javascriptc#.netkendo-uikendo-asp.net-mvc

Compare Server Time and Browser Time


My table that has 3 columns: FileName, LastUpdateTime, and a Restart button.

I need to display the restart button only if the last update time is more than 20 min ago. I get the last update time from the server. d = new Date() gives the local browser time but the lastUpdateTime is coming from the server. Server is in the different time zone than the clients browser.

The following code works if both server and browser are in the same time zone. Do you have any suggestions on how solve this if the server and browser are in a different time zone?

This application supposed to run anywhere in US and Europe.

var lastUpdatedTime = (gridData[i].LastTimeUpdated);
var d = new Date();
//deducting 20 min from current time
var deductTwenty = d.setMinutes(d.getMinutes() - 20); 
var parsedupdatetime = Date.parse(lastUpdatedTime);                          

// If the last update time is not 20 ago, hide it. 
if (parsedupdatetime > deductTwenty) {
    newrestartButton.hide();
}

Solution

  • Use .NET in your .cshtml file to get the date server-side. Assuming you use MVC (since you tagged this question kendo-asp.net-mvc).

    @{
        var deductTwenty = DateTime.Now.AddMinutes(-20);
    }
    
    <script>
        var jsDeductTwenty = new Date(@deductTwenty.Year, @deductTwenty.Month-1, @deductTwenty.Day, @deductTwenty.Hour, @deductTwenty.Minute);
    </script>
    

    Result:

    JS Time