Tuesday, 2 April 2019

How to store DateTimeOffset and Display local Datetime - Working is DateTimeOffset in .Net, MVC

Recently i have been working on a website which might get used globally and requires datetime to be displayed according to User.

There are two things happening in the application:

1. There is Feed coming from different sources which contains DateTime. I am saving that DateTime in SqlServer as DateTimeOffset. Sql server stores it like : 2019-04-02 11:24:00.0000000 +00:00.

2. Now i have MVC application where i would like to display date time in local format, i live in London so in addition to normal DateTime conversion, i also face the challenge of Daylight saving times. All that can be handled using JavaScript. Following is what i am doing. I am creating a Javascript object "new Date" then converting that to Locale string, here JS will use user's Locale and show the DateTime.

 var latestate = new Date('@Model.LatestItemDateTime.ToLocalTime().ToString("dd/MM/yyyy hh:mm:ss")').toLocaleString();


It seems to work and i feel it's simpler solution. This might not fit in every development project it gives an overview of how Javascript can handle locale.        

No comments:

Post a Comment