I have MVC 5 application. I have the following workflow:
Select a time from kendo time picker control -> On server converts that time into UTC -> Store UTC time in the database -> Retrieve UTC time from DB -> Bind it back to time picker control
Server’s time zone is Central Standard Time (UTC -6:00)
I have the following steps:
1> User selects a time from the Kendo Time picker control and submits it to the server. For example assume user picks “6:00 PM”
2> On server when I check the posted date value, the Kind property is unspecified
. That means if I convert this time to UTC time its going to use server’s time zone to offset the time. However i think we want client’s timezone to offset.
3>So I thought of using Kendo time picker’s value() method which returns the value in string format as Wed May 18 2016 18:00:00 GMT-0500 (Central Daylight Time)
. This value has time zone info including day light saving.
4>So I added hidden field and on client side I assign string value to hidden field.
5> On server I use the hidden field value to convert into UTC and store it into database. So the above value is stored as 23:00:00 UTC time. ( offset is -5)
So far so good
6> On some other screen now I want to show this value to client.
7> Now when i bind this UTC time back to time picker control, it shows 5:00 PM instead of 6:00 PM ( that is 17:00:00 instead of 18:00:00). That means its offsetting by -6 (that’s Central Standard Time).
Question:
1> When getting the value from time picker it knows the day light time zone. ( as we see in the string value). However when we set the value its not considering day light time zone. Why?
Model
public class Mymodel
{
[DataType(DataType.Time)]
public DateTime OrderTime { get; set; }
public string OrderTimeHidden { get; set; }
}
View: That captures time
@model MyNameSpace.Mymodel
<div>
@(Html.Kendo().TimePickerFor(x => x.OrderTime)
.Format("hh:mm tt")
.Value("08:00 PM")
.HtmlAttributes(new { onkeydown = "javascript:return false;" })
)
@Html.HiddenFor(x=>x.OrderTimeHidden)
</div>
Javascript: That post the model to server
submit.click(function () {
$('#OrderTimeHidden').val($("#OrderTime").getKendoTimePicker().value());
// do post here
$.ajax({...})
})
Controller: That converts and saves time into database
[HttpPost]
public ActionResult Save(MyModel model)
{
var dt = DateTime.ParseExact(model.OrderTimeHidden.Substring(0, 33), "ddd MMM d yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture);
var entity = new MyEntity();
//OrderTime on entity is of type TimeSpan
entity.OrderTime = dt.ToUniversalTime().TimeOfDay;
SaveToDataBase(entity)
return View("SomeView");
}
Controller: That shows the time on UI
[HttpGet]
public ActionResult ViewTime()
{
var entity = GetEntityFromDataBase();
var model = new MyModel();
model.OrderTime = new DateTime(entity.OrderTime.Ticks, DateTimeKind.Utc)
return View();
}
View: That shows the time on UI
<div>
@Html.Kendo().TimePickerFor(x => x.OrderTime)
.Format("hh:mm tt");
</div>