Kendo Grid Automatically changing Timezone
Asked Answered
L

6

6

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);
Literality answered 23/9, 2013 at 8:51 Comment(0)
L
3

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.

Literality answered 8/10, 2013 at 8:36 Comment(4)
Bear in mind that in this case filtering with the date picker control will not work though.Fideicommissary
Did you mean columns.Bound(p => p.x);?Globe
Also the field is not editable by using this approachNote
@Globe filtering with datePostern
S
5

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

Sherrisherrie answered 1/10, 2013 at 6:13 Comment(1)
This is fine in this scenario but not a right solution for all scenarios, lets say I have a Date of birth field hypothetical 1/1/1970 or 12/31/1970 when you change it to UTC to store it will change the date and retrieving again in UTC will also not work. Wondering if Kendo dont' have a fix/workaround on this.Feuchtwanger
L
3

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.

Literality answered 8/10, 2013 at 8:36 Comment(4)
Bear in mind that in this case filtering with the date picker control will not work though.Fideicommissary
Did you mean columns.Bound(p => p.x);?Globe
Also the field is not editable by using this approachNote
@Globe filtering with datePostern
H
3

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;
Horta answered 27/7, 2019 at 4:13 Comment(0)
K
1

Another option is to use custom JsonResult and convert the date to ISO format.

public class IsoDateJsonResult : JsonResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        HttpResponseBase response = context.HttpContext.Response;

        if (!String.IsNullOrEmpty(ContentType))
        {
            response.ContentType = ContentType;
        }
        else
        {
            response.ContentType = "application/json";
        }
        if (ContentEncoding != null)
        {
            response.ContentEncoding = ContentEncoding;
        }
        if (Data != null)
        {
            var isoConvert = new IsoDateTimeConverter();
            response.Write(JsonConvert.SerializeObject(Data, isoConvert));
        }
    }

Then change your Controller method to return IsoDateJsonResult instead of ActionResult/JsonResult.

Karajan answered 18/8, 2016 at 5:35 Comment(0)
H
1

Solution 2 in my above answer, daylight saving hour is getting added if you are not in daylight saving period but you are trying to access the date which falls in daylight saving period, rewriting the solution 2 to support daylight saving period aswell

Client side code to update timezone name:

    $.ajax({
            type: "POST",
            url: '@Url.Action("Controller action method name", "Controller name")',
            data: { "timeZoneName": Intl.DateTimeFormat().resolvedOptions().timeZone },
            success: function (data) {
                    }
            });

Controller method name to update the timezone in session:

   public ActionResult actionMethod(string timeZoneName)
    {
        Session["timeZoneName"] = Convert.ToString(timeZoneName);
        return Json(new { success = true });
    }

App config app setting entries:

<add key ="Europe/London"  value ="GMT Standard Time" />

Here the key is client time zone name returned by browser and maintained in session here, we have to add entries for all time zone

Place the below code in the controller action method to get the exam date:

var clientMachineTimeZoneName = Convert.ToString(Session["timeZoneName"]);

Get the sever timezone id from config for the corresponding time zone which we got from client side
var timeZoneId = ConfigurationManager.AppSettings[clientMachineTimeZoneName];

TimeZoneInfo clientTimezoneDetails = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

var clientTimeZoneOffsetMinutes = clientTimezoneDetails.GetUtcOffset(x.ExamDate.Value).TotalMinutes * -1;
var serverAndClientMachineTimeZoneDifferenceInMinutes = clientTimeZoneOffsetMinutes + TimeZoneInfo.Local.GetUtcOffset(x.ExamDate.Value).TotalMinutes;

//Update your date time field with this offset minutes
ExamDate = ExamDate.Value.AddMinutes(serverAndClientMachineTimeZoneDifferenceInMinutes);
Horta answered 21/10, 2019 at 12:20 Comment(0)
A
0

In my case the server is in CST and I am in MST. I needed to persist my SQL Server data to the browser and was getting 02/08/18 23:57 as 02/08/18 22:57 on my Kendo Grid. So I did this, hope it helps:

Checks User / Browser's timezone offset

Gets difference in hours from server timezone offset

Looks at a column on the Kendo Grid with class .dbDate

Grabs the date in that cell (displayedTime) from data object

Uses Moment.js to Convert (convertedTime) it based on the difference (diff) in hours we pass it.

Formats convertedTime to desired format i.e. 02/08/18 23:57

Add 1 to i so the next date in the object gets adjusted

Passes the Grid back the updated date and time.

Must run last on page/grid load/update.

function getDateOffset() {
    var date = new Date();
    var offset;
    var diff;
    offset = date.getTimezoneOffset()
    if (offset > 360) { //360 = CST
        diff = +(offset - 360) / 60
    } else if (offset < 360) {
        diff = -(360 - offset) / 60
    } else {
        diff = 0
    }

    $(".dbDate").each(function (i) {

        var grid = $('#Grid').data('kendoGrid');
        var displayedTime = grid.dataSource.data()[i].TicketDateTime
        var convertedTime = new moment(displayedTime).add(diff, 'hours').toDate();
        var originalTime = moment(convertedTime).format("MM/DD/YY HH:mm");

        i + 1
        $(this).html(originalTime)
    })
}
Absorber answered 21/2, 2018 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.