0

I'm currently writing a small messaging app for global usage. I'm going to store the UTC timestamp of the message. I need to display the message to the user using AJAX, so i need to convert the UNIX timestamp to the users local time. I know how to get the timezone offset, but i'm afraid that it will be a little bit inaccurate and i need accuracy even in seconds (to display times like: 34 seconds ago). Which is the most accurate way to solve this?

2
  • My first tough was to compare the Date() and getUTC() values to calculate offset. Don't really know if that would be apporate. Commented Jan 21, 2014 at 13:40
  • var date = new Date(UTC+"000") Commented Jan 21, 2014 at 13:41

2 Answers 2

1

Why not calculate the local date locally? Constructing a date with new Date(millisecondsSinceEpoch) locally and then calling toTimeString() (or whatever) should show you the local time:

new Date(milliseconds).toTimeString()
Sign up to request clarification or add additional context in comments.

Comments

0
var d = new Date((new Date(1390312399000)).toUTCString());

Where the timestamp is the stored UTC timestamp.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.