Articles in this section
Category / Section

How to convert dates to UTC format

1 min read

Summary:

 

The Dates retrieved from the database will be converted based on the local time zone in both client and server end.  Converting the Dates to UTC format will prevent the time offsets difference between server and client time zones.

Solution:

 

To prevent the Date conversion and to display all the dates in UTC format, the following steps have to be done on both client and server side.

 

Step 1:

The dates from the database must be returned in UTC format. To do that, set the DateTime Kind property to UTC. Refer to the following code,

[Controller]

public ActionResult Index()
{
    List<GridObject> dates = new List<GridObject>();
    dates.Add(new GridObject { Id = 1,  Date = new DateTime(2018, 3, 1, 14, 00, 00, DateTimeKind.Utc) });
    dates.Add(new GridObject { Id = 2,  Date = new DateTime(2018, 3, 15, 14, 00, 00, DateTimeKind.Utc) });
    ViewModel model = new ViewModel();
    model.Data = dates;
    return View(model);
}

 

Now the Dates will be returned to the view in UTC format.

Step 2:

When the returned dates are parsed in client end using “new Date()” method, it will be converted to client’s time zone. To avoid that, set ej.parseDateInUTC to true. Refer to the following code,

[View]

<script>
    ej.parseDateInUTC = true;
</script>

 

Now the all the Dates will be displayed in UTC format.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied