0

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>
LP13
  • 30,567
  • 53
  • 217
  • 400
  • I have not had a problem however I convert from UTC to user's time zone just before it gets to the view. Vice versa, certain saves will convert the time sent in to the UTC equivalent, however, for this to work you have to store the user's time zone info as the client should not be used to calculate timezone math. – Ross Bush May 19 '16 at 02:05
  • Thats the problem, How do you know the user's timezone on server? You dont. However browser knows the timezone. So Timepicker should use broswer's timezone and convert it accordingly. – LP13 May 19 '16 at 02:07
  • You can't rely on the user having the correct client settings for their TimeZone. If it is that critical then have them pick a TimeZone from a dropdown where appropriate. – Ross Bush May 19 '16 at 02:08
  • Or just assume everyone lives in the same time zone as your server :) – Ross Bush May 19 '16 at 02:10
  • Then make your date time picker in the view be agnostic to anything related to time zone. The selection made should be in what the user thinks their time is, if that makes sense. – Ross Bush May 19 '16 at 02:19
  • welll this is not public web site, so assume user will have correct time zone selected on their machine. Also @lrb your answer doesnt answer my question. – LP13 May 19 '16 at 03:56

0 Answers0