How do you convert a JavaScript date to UTC?

Suppose a user of your website enters a date range.

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but the server expects all dates and times to be in UTC.

Now suppose the user is in Alaska or Hawaii or Fiji. Since they are in a timezone quite different from UTC, the date range needs to be converted to something like this:

2009-1-1T8:00:00 to 2009-1-4T7:59:59

Using the JavaScript Date object, how would you convert the first “localized” date range into something the server will understand?

103 thoughts on “How do you convert a JavaScript date to UTC?”

  1. Great job site admin! You have made it look so easy talking about Blogging, providing your readers some vital information. I would love to see more helpful articles like this, so please keep posting! I also have great posts about the subject, check out my weblog at xrank.cyou

    Reply

Leave a Comment