I have a date-time string in a particular timezone, and I need to convert it to the local time. For example, I have the time “Feb 28, 2013, 7:00 PM ET”, and I want to create a JavaScript Date object that reflects this date and time in the JavaScript Date timezone.
I’ve been able to set the year, month, date, and hours with the following code:
var mydate = new Date();
mydate.setFullYear(2013);
mydate.setMonth(02);
mydate.setDate(28);
mydate.setHours(7);
mydate.setMinutes(00);
But, I’m not sure how to set the time in a specific time zone, especially considering daylight saving time adjustments. I’ve considered adding or subtracting the UTC offset, but I’m not sure how to account for daylight savings. What’s the best approach to convert time from a different time zone to local time in JavaScript?
I’ve worked a lot with date formatting in international apps, and honestly, if you’re looking to format a JavaScript Date object to a specific time zone, Intl.DateTimeFormat
is a great starting point. It doesn’t modify the original Date object, but it lets you view it as if it were in another time zone.
var dateStr = "Feb 28, 2013, 7:00 PM";
var options = { timeZone: 'America/New_York', hour12: true };
var formatter = new Intl.DateTimeFormat('en-US', options);
var date = new Date(dateStr);
console.log(formatter.format(date)); // Displays date in America/New_York time
The best part? It handles daylight saving time for you. So while it doesn’t change the core Date object’s time zone, it lets you view it as if it were in that zone. If you need more control over the Date itself though… read on.
Right, @jacqueline-bosco approach works perfectly if you just want to display the time. But if you’re like me and need to manipulate or calculate with the actual Date values, then you’ll want to work with UTC and manually adjust the offset. Here’s how I usually go about initializing a JavaScript Date object to a specific time zone when I need to control the internal time directly.
var mydate = new Date(Date.UTC(2013, 1, 28, 19, 0)); // 7 PM UTC
var timeZoneOffset = -5; // Eastern Time (without DST)
mydate.setHours(mydate.getHours() + timeZoneOffset);
console.log(mydate);
This method creates a Date in UTC and then adjusts it. But warning: it doesn’t handle daylight saving automatically—you’d have to detect that separately or hard-code around it. It gives you control but at a cost. If you want something smarter and smoother, go with a library.
Exactly! If you want zero headache and full control, I’d say—just use moment-timezone
. I’ve been using it for years in production apps, especially when working with international users. It allows you to both parse and format a JavaScript Date object to a specific time zone without fiddling with offsets manually.
var moment = require('moment-timezone');
var dateStr = "Feb 28, 2013, 7:00 PM";
var myDate = moment.tz(dateStr, "America/New_York");
var localDate = myDate.local();
console.log(localDate.format());
This one-liner does it all: parsing, time zone handling, daylight saving, and formatting. It’s especially useful if you’re dealing with multiple time zones or server-client differences. Just make sure to include the moment-timezone package in your project…