Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kendo Grid Automatically changing Timezone

On my Kendo Grid I recieve date time from server. On the client end, this time is changed to client's timezone and that is displayed. How can I show the same time from the server to the client.

the following is my kendo code for binding the datetime.

columns.Bound(p => p.CreateDate).Format("{0:dd/MM/yyyy hh:mm:ss}").Sortable(true).Width(180);
like image 400
Raju S Nair Avatar asked Sep 23 '13 08:09

Raju S Nair


3 Answers

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

like image 118
SimonGates Avatar answered Nov 25 '22 07:11

SimonGates


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 a string, so that it won't get converted to server's time (UK) while saving to the database.

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 scenarios.

Before you return the DataSource result from the controller action method to kendo grid, do the operation below to 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;
like image 45
Rex Andrew Avatar answered Nov 25 '22 09:11

Rex Andrew


This was my solution.

In the controller I did this:

DateTime time = DateTime.Now();

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

And in the View:

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

It is also sortable.

like image 21
Raju S Nair Avatar answered Nov 25 '22 07:11

Raju S Nair