24

I have a date given to me by a server in unix time: 1458619200000

NOTE: the other questions you have marked as "duplicate" don't show how to get there from UNIX TIME. I am looking for a specific example in javascript.

However, I find that depending on my timezone I'll have two different results:

d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)

// Now I set my computer to Eastern Time and I get a different result.

d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)

So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • 2
    Check out: http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s – roland Mar 24 '16 at 17:22
  • i think this is what you want: http://stackoverflow.com/questions/20834411/specify-timezone-in-javascript – messerbill Mar 24 '16 at 17:22
  • I looked through all those but didn't understand it to well. I'd like a code example for what I'm trying to do specifically... they don't answer that. – Shai UI Mar 24 '16 at 17:23
  • 1
    Possible duplicate of [How to convert datetime from the users timezone to EST in javascript](http://stackoverflow.com/questions/9070604/how-to-convert-datetime-from-the-users-timezone-to-est-in-javascript) – Trevor Clarke Mar 24 '16 at 17:24
  • This one is right http://stackoverflow.com/questions/20834411/specify-timezone-in-javascript/ – Asim Khan Mar 24 '16 at 17:26
  • Seems like you are looking for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString . – Felix Kling Mar 24 '16 at 17:28

3 Answers3

24

You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,

var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)

dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)

var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)

Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!

Community
  • 1
  • 1
Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52
  • is your unix time always in EST timezone? If so then you need to add the correct GMT offset for your input (-4 hours), then translate THAT to the local time zone by adding the local timezoneoffset() back into the unix time before creating a new date object. – Patrick Gunderson Mar 24 '16 at 17:44
  • what are you setting the time to, utc? how do I set it to a specific timezone – Shai UI Mar 24 '16 at 17:46
  • @foreyez From the UTC time, its easy to convert to any fixed timezone. You can simply add or subtract the timezone offset in the date object by using `setTime()`. For example, if your fixed timezone is UTC+10 (Australian Eastern Standard Time), `+600*60*1000` will be the offset you can add to your time. – Shekhar Chikara Mar 24 '16 at 17:49
  • 1
    although it's easier to just d.setHours(d.getHours() - 5); to convert it to eastern – Shai UI Mar 24 '16 at 18:27
  • 14
    This does not take into account daylight savings. For instance EST is -5 hrs in the winter and -4 hrs in the summer – Bagelstein Dec 05 '19 at 16:03
12

Moment.js (http://momentjs.com/timezone) is your friend.

You want to do something like this:

var d = new Date(1458619200000);
var myTimezone = "America/Toronto";
var myDatetimeFormat= "YYYY-MM-DD hh:mm:ss a z";
var myDatetimeString = moment(d).tz(myTimezone).format(myDatetimeFormat);

console.log(myDatetimeString); // gives me "2016-03-22 12:00:00 am EDT"
Ashwin Balamohan
  • 3,303
  • 2
  • 25
  • 47
  • 21
    I don't want to use moment – Shai UI Mar 24 '16 at 17:28
  • 2
    yes but it adds a problem for me to download library, add it to my source code, change all my existing code to fit with it, etc etc as opposed to what I'm looking for: which is two lines of javascript that I can't figure out. – Shai UI Mar 24 '16 at 17:31
  • 1
    however +1 your answer bc I'll use this if I don't get any other answers :P – Shai UI Mar 24 '16 at 17:35
  • Thank you. You can write your own function that does this, but it would involve reinventing the wheel, somewhat. See http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329/ – Ashwin Balamohan Mar 24 '16 at 17:36
  • note: this didn't work. even if I use the example above, the time changes based on my local timezone. I think it's a problem with "new Date" – Shai UI Mar 24 '16 at 17:48
  • 2
    For my codebase, moment + moment-timezone has added 500kb to the size of the application since it's not tree shakable. If you want smaller bundle sizes, moment may not be the best solution to use. – bmarti44 Sep 10 '18 at 15:51
  • For this to work, you have to know your timezone and hard code it? – Dan Chase Apr 29 '21 at 22:34
3

For daylight saving, Eastern time become 4 hours behind UTC. That's why its offset is -4x60 = -240 minutes. So when daylight is not active the offset will be -300. The offset variable's value is the key point to be noted here. Kindly see this code in action in attached image.

var offset = new Date().getTimezoneOffset();// getting offset to make time in gmt+0 zone (UTC) (for gmt+5 offset comes as -300 minutes)
var date = new Date();
date.setMinutes ( date.getMinutes() + offset);// date now in UTC time
            
var easternTimeOffset = -240; //for dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300
date.setMinutes ( date.getMinutes() + easternTimeOffset);

You can see this code in action here in this image

Akaisteph7
  • 5,034
  • 2
  • 20
  • 43
Hasan Badshah
  • 773
  • 1
  • 6
  • 16