3

I have a module in my website where logged in users can post comments on any article. I am saving UTC time as posted time in database. As per my requirement I need to display posted time for comment and the time difference between posted time and current time (for example last seen in Facebook or comment posted time in Facebook) with the comment. The posted time must show in Client Browser's timezone.

For e.g: User XYZ has commented on 01:00 UTC, and

  • If account of XYZ is logged in from India then timezone must be India Time Zone (UTC+05:30) and time will appear 6:30 AM.

  • If account of XYZ is logged in from Singapore then timezone must be Singapore Time Zone(UTC+8:00) and time will appear 9:00 AM.

To achieve above I have searched on web and found this post get client time zone from browser which was very helpful to get client browser's timezone. (Which I am not able to test yet if it will convert the time correctly or not)

Now the main issue is to convert UTC time to given timezone (which I am getting using solution mentioned here get client time zone from browser).

Please share suggestions.

Community
  • 1
  • 1
NMathur
  • 829
  • 1
  • 17
  • 35
  • You're going to have to convert Olson time zones to Windows time zones as described here: http://stackoverflow.com/questions/5996320/net-timezoneinfo-from-olson-time-zone. After you do that, converting the `DateTime` from UTC should be simple. – Andrew Whitaker Aug 18 '14 at 14:05
  • @AndrewWhitaker thanks for reply .. the solution is using C# .. I need to perform all operations in JavaScript. Please suggest ... – NMathur Aug 19 '14 at 03:57
  • 1
    Here is a javascript [answer](http://stackoverflow.com/q/6525538/2030565) – Jasen Aug 19 '14 at 05:16

2 Answers2

0

If you get the name of the timezone, 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.

lopezbertoni
  • 3,551
  • 3
  • 37
  • 53
  • @lopzbertoni .. thanks for your reply, I have to perform the complete operation using java-script so that it can run faster. Please suggest .. – NMathur Aug 19 '14 at 03:52
  • You can use @Jasen suggestion. Or else you need to store time in UTC as well as the UTC offset and if the time is Daylight Savings too to recreate local time properly. – lopezbertoni Aug 19 '14 at 14:00
0

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

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

Hope it will help you.

Community
  • 1
  • 1
Neeraj Purohit
  • 243
  • 3
  • 23