Convert UTC time to client browser timezone using JavaScript in MVC view

I have a module on my website where registered users can comment on any article. I am storing UTC time

as published time in the database. As per my requirement, I need to show posted time

for comments and time difference

between the published time and the current time (like last seen on Facebook or comment posted on Facebook) with the comment. The marked time should be displayed at Client Browser timezone

.

For example, user XYZ commented 01:00 UTC

and

  • If the XYZ account is registered in India, then the time timezone

    should be India Time Zone (UTC+05:30)

    6:30 AM.

  • If the XYZ account is registered in Singapore, then the time zone should be Singapore Time Zone(UTC+8:00)

    and the time will be 9:00 AM.

To achieve the above, I searched the web and found this post to get the client timezone from the browser , which helped a lot to get the client browser timezone

. (Which I still can't check if it will convert time correctly or not)

Now the main problem is converting UTC time to the given one timezone

(which I get with the solution mentioned here to get the client timezone from the browser ).

Please share your tips.

+3


source to share


2 answers


If you got the timezone name, you can get the time from UTC like this:

var utc = DateTime.UtcNow;
var tz = TimeZoneInfo.FindSystemTimeZoneById("The Timezone");
return TimeZoneInfo.ConvertTimeFromUtc(utc, tz);

      



Hope this helps.

0


source


You can get the client browser offset value from this snippet JS

.

function returnTimeDiff(postDateTime, spanid) {
    var offset = (new Date().getTimezoneOffset() / 60)
}

      



Hope this helps you.

0


source







All Articles