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);
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
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With