For example, your client computer is located in Sydney, and your code is deployed in India
Saving date and time in the database: When transferring date and time from the client side (javascript) to the server (.net), pass it as a string so that it does not convert to server time (Great Britain) when saving to the database
If your date and time field is not editable, follow solution 1, otherwise solution 2 would be the right choice
Extract from the database :
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:
Extract from the database:
Client Side Code:
$.ajax({ type: "POST", url: '@Url.Action("Controller action method name", "Controller name")', data: { "clientMachineTimeZoneOffsetInMinutes ": (new Date()).getTimezoneOffset() }, success: function (data) { } });
Server Code:
// Server time zone (India) Offset minutes: 330
// Client Time Zone (Sydney) Offset minutes: -600
// The difference between the time zone of the client and server 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);