2

Hi,
from the client side I'm sending to the server data with current date in ISO format. I'm using new Date().toISOString() for composing datetime before I send it to the server. When I get all my data back from server to client I receive datetime in the server's timezone.

For example:
on the server I have such date value - "2014-02-16T03:10:13.383"
on the client should be converted to local time - "2014-02-16T13:10:13.383"

How should I convert datetime from server to the client's local time?

devger
  • 703
  • 4
  • 12
  • 26
  • and you want to do that with javascript? `var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() ` – Jorge Y. C. Rodriguez Feb 16 '14 at 11:37
  • You might be better off passing an epoch timestamp. That won't need too much manupulation – Lix Feb 16 '14 at 11:38
  • @josser Please let me know if my answer worked for you or if you have any issues with the answer. Please accept my reply as correct answer if it worked for you- so that other users can benefit: from knowing that the answer works and by having the question marked as Answered. – DhruvJoshi Feb 17 '14 at 13:05

1 Answers1

0

The handling is usually two fold. To handle it on server side, you'll need reliable time zone information. This is usually obtained from the user's preferences for time zone in their profile page.

Having said that we need to make sure that all date data captured on client side and when stored in DB is in UTC format. JS functions have a ready support for the UTC datetime.

When you store you use UTC() method in JS to get date.

When displaying back either you supply the time-zone equivalent from DB(based on stored user time zone data) or use JS to get it from the browser locale like this

<script type="text/javascript">
        function showDateInClientSideFormat(dValue)
        {
            var d = new Date()
            var n = d.getTimezoneOffset();
            var dateClientSide = new Date(dValue +n);
            return dateClientSide;
        }
    </script>

Also see my answer here https://stackoverflow.com/a/21573366/1123226 and another great SO link How can I handle time zones in my webapp?

Community
  • 1
  • 1
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60