Kendo Grid Automatically changing Timezone

Since the dates are created on the client when the response from the server is returned - the dates are always created with an offset according to the timezone of the browser

This will help you:

http://www.kendoui.com/code-library/mvc/grid/using-utc-time-on-both-client-and-server-sides.aspx


This was my solution

on controller I did like this

DateTime time = DateTime.Now();

string x = time.ToString("MM/dd/yyyy hh:mm:ss tt");

On View

columns.Bound(p => p.x);

It is sortable also.


For example your client machine is in Sydney and your code is deployed in India

Saving datetime to DB: While passing the date time from client side(javascript) to server(.net) pass it as string, so that it won't get converted to server's time(UK) while saving into DB

If your datetime field is non editable then follow solution 1 otherwise solution 2 would be the right choice

Retrieving from DB:

Solution 1:

Client side Code:

cols.Bound(c => c.ExamDate).ClientTemplate(("#= ExamDateString #")).Hidden(false).Filterable(x => x.Cell(cell => cell.ShowOperators(false).Operator(StringOperator.eq.ToString())));

Server Side Code:

Server Model property for format

public string ExamDateString 
    { 
        get 
        { 
        return ExamDate.HasValue ? ExamDate.Value.ToString("dd/MM/yyyy hh:mm:ss") : string.Empty; 
        } 
    }

Solution 2:

Retrieving from DB:

Client side code:

$.ajax({ 

            type: "POST", 

            url: '@Url.Action("Controller action method name", "Controller name")', 

            data: { "clientMachineTimeZoneOffsetInMinutes ": (new Date()).getTimezoneOffset() }, 

                success: function (data) { 

                    } 

         });

Server side code:

//Server Timezone(India) Offset minutes : 330

//Client Timezone(Sydney) Offset minutes : -600

//Difference between Client and Server timezone offset minutes = -270

var serverTimeZoneOffsetInMinutes = DateTimeOffset.Now.Offset.TotalMinutes;
var serverAndClientMachineTimeZoneDifferenceInMinutes = clientMachineTimeZoneOffsetInMinutes + serverTimeZoneOffsetInMinutes; 

//Update your date time field with this offset minutes
ExamDate = ExamDate.Value.AddMinutes(serverAndClientMachineTimeZoneDifferenceInMinutes);

Solution 3:
Solution 2 won't handle daylight saving scenario, This would be the ideal solution to handle all the scenario.

Before you return the DataSource result from the controller action method to kendo grid, do the below operation it would stop the conversion

       var response = new ContentResult
        {
            Content = JsonConvert.SerializeObject(value, new JsonSerializerSettings
            {
                DateTimeZoneHandling = DateTimeZoneHandling.Local,
                DateFormatString = "yyyy-MM-ddTHH:mm:ss"
            }),
            ContentType = "application/json"
        };

        return response;