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;