1

I have unix TimeStamp value which is in UTC . I want to convert that timestamp into local date and time with timezone.

By using the following code, I am manged to covert to UTC date and time

function convertTimestamp(timestamp) {
  var d = new Date(timestamp * 1000),
    yyyy = d.getFullYear(),
    mm = ('0' + (d.getMonth() + 1)).slice(-2),
    dd = ('0' + d.getDate()).slice(-2),
    hh = d.getHours(),
    h = hh,
    min = ('0' + d.getMinutes()).slice(-2),
    time;
    time = yyyy + '-' + mm + '-' + dd + ', ' + h + ':' + min + ' ';
  return time;
}

Example:

3680283088 => 05/10/2017 13:46:49 EDT

Please recommend the right way to convert UTC timestamp to local date and time with timezone.

Renganathan M G
  • 5,039
  • 2
  • 31
  • 35
  • timestamp `3680283088` gives a date in the year 2086, are you sure that's the correct value? –  Aug 16 '17 at 15:46
  • Its just a dummy value – Renganathan M G Aug 16 '17 at 15:57
  • Without any external libraries it's not impossible, but still a very difficult task. The problem with `get` methods is that they return the values in the timezone configured in the client's system/browser. So your approach will work only if the client's system timezone is in `EDT`, otherwise you'll get wrong results. One alternative is to get the UTC values for the fields, using `getUTC` methods, such as `getUTCFullYear()`, `getUTCHours()` and so on. Then, assuming that `EDT` is "Eastern Daylight Time", we subtract 4 hours (as `EDT` is 4 hours behind UTC). –  Aug 16 '17 at 18:49
  • But this subtraction is full of corner cases: If the hour is 1 AM, subtracting 4 hours will result in 9 PM of the previous day; But if the day is 1st of the month, the result will be in the previous month (and you can also end up in the previous year); You must also take care of leap years; And there' also Daylight Saving Time issues (you'll need the whole history of offset changes for this timezone). Although all these math is possible to be done, it's too hard to handle by yourself. Do you really need to **not** have any external library? –  Aug 16 '17 at 18:49
  • Do I have to use libraries like moment.js ?? – Renganathan M G Aug 16 '17 at 19:35
  • 2
    In this case, I think it's totally worth using a library - I personally like [moment.js](https://momentjs.com). To handle timezones, you'll also need [moment timezone](https://momentjs.com/timezone). Another detail is that short names like `EDT` are [ambiguous and not standard](https://stackoverflow.com/a/18407231/7605325). Prefer to use [IANA timezones names](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) (always in the format `Region/City`, like `America/New_York` or `Europe/Berlin`) –  Aug 16 '17 at 19:42
  • Also note that `EDT` is used only during Daylight Saving Time. When it's not in DST, `EST` is used instead (and the offset is -5 instead of -4). When using timezones full names (like `America/New_York`), the API takes care of this automatically, because it already has the history of all offsets and the dates when changes occur. –  Aug 16 '17 at 19:44
  • 1
    @Hugo - in this case, the abbreviation is part of a display value, so it's not really a place for a tzid. However, some abbreviations are indeed ambiguous, and the tzdb has been removing "invented" abbreviations, replacing them with numerical offsets. So yes, there's quite a lot of gotcha's with this. Also, don't forget about Intl/402, as I showed in my answer. It's mostly everywhere now, so good time to start using it. :) – Matt Johnson-Pint Aug 16 '17 at 20:05
  • @MattJohnson According to docs, moment timezone has support to abbreviations, though I haven't tested it. Anyway, I still think it's worth using a library for this (while we don't have decent native support). –  Aug 16 '17 at 20:51
  • 2
    Yep, it does (I maintain it). And agree with you. Just the OP asked for a solution without external dependencies. – Matt Johnson-Pint Aug 16 '17 at 20:52
  • @MattJohnson Wow, I didn't know you're a maintainer. So thanks for providing such a great API! –  Aug 16 '17 at 21:42

1 Answers1

3

If the environment fully implements the ECMA-402 specification, you may be able to use toLocaleString with some options as follows:

var d = new Date(yourTimestamp);
var s = d.toLocaleString('en-US', {hour12: false, timeZoneName: 'short'}).replace(',','');
// example: "8/16/2017 12:58:07 PDT"

Note that the replace is because on my Chrome browser, there is a comma between the date and time portion, and you didn't ask for one. Also note that other implementations may vary, so this isn't guaranteed to give you the exact same output everywhere.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575