Convert Epoch time to human readable with specific timezone

To convert epoch dateTime to human readable , using a simple new date(1495159447834) will suffice.

The problem I'm encountering now is that for my hybrid application, if the user set the time-zone in his phone date time setting to lets say GMT +12:00 ,the human readable dateTime will be different from what I would want the user to have and I would want him/her to follow the server timezone.

Thus , how would I convert the epoch number to a specific given timezone in a human readable format.

I have tried example like:

var test= new Date('1495159447834 GMT+0800').toString();

and it returns me an Invalid Date.

If possible, I would want this without any libraries. I have looked through the answers here and I believe that I could not find any answers I'm looking for. If there is any previously answered question with the same topic, do let me know and I will close this question!

Answers:

Answer

You can use offset to convert your current datetime to a specific timezone.

function convertEpochToSpecificTimezone(offset){
    var d = new Date(1495159447834);
    var utc = d.getTime() + (d.getTimezoneOffset() * 60000);  //This converts to UTC 00:00
    var nd = new Date(utc + (3600000*offset));
    return nd.toLocaleString();
}

The offset will be your specific timezone. Example: GMT +03:00, your offset is +3. If GMT -10:00, offset is -10

Answer

there are number of ways to convert between Epoch and Human readable format

//Convert epoch to human readable date
var myDate = new Date( 1533132667*1000);
document.write(myDate.toGMTString()+"<hr>"+myDate.toLocaleString());

//this will return Wed, 01 Aug 2018 14:11:07 GMT

  //Convert human readable dates to epoch
var myDate = new Date("Wed Aug 01 2018 14:11:07 GMT"); 
var myEpoch = myDate.getTime()/1000.0;

// this will return 1533132667

For Reference: https://www.epochconverter.com/programming/#javascript

Edit# added a JSFiddle here

Answer

set the initial date to the epoch and add UTC units. Say you have a UTC epoch var stored in seconds. How about 1234567890. To convert that to a proper date in the local time zone:

var utcSeconds = 1234567890;
var d = new Date(0); // The 0 there is the key, which sets the date to 
the epoch
d.setUTCSeconds(utcSeconds);

Or you can use momentjs

moment.unix(yourUnixEpochTime).format('dddd, MMMM Do, YYYY h:mm:ss A')

or you can use this way

var dateVal ="/Date(1342709595000)/";
var date = new Date(parseFloat(dateVal.substr(6)));
document.write( 
    (date.getMonth() + 1) + "/" +
    date.getDate() + "/" +
    date.getFullYear() + " " +
    date.getHours() + ":" +
    date.getMinutes() + ":" +
    date.getSeconds()
);

Answer

This is old, but I did it this way:

function formatDate(date, includeTime) {
  const dateTimeFormat = new Intl.DateTimeFormat('en-US', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    timeZone: 'America/Los_Angeles',
    timeZoneName: 'short',
  });
  const [
    { value: month },
    ,
    { value: day },
    ,
    { value: year },
    ,
    { value: hour },
    ,
    { value: minute },
    ,
    { value: dayPeriod },
    ,
    { value: timeZoneName },
  ] = dateTimeFormat.formatToParts(date);
  if (includeTime) {
    return `${day} ${month} ${year} • ${hour}:${minute}${dayPeriod.toLowerCase()} ${timeZoneName}`;
  }
  return `${day} ${month} ${year}`;

This will output the time of the given timezone.

For example, if I have an epoch time (unix timestamp) and if I'm in Argentina, the time should be displayed as 03:45 GMT -3 of June 2, but with this code, it will be displayed as the time that should be displayed for Los Angeles. My requirement was to display time in Los Angeles timezone, even if I visit the page from Argentina.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.