0

I have time that is represented in response as: 1386180000 and in javascript it adds timezone, as: Wed Dec 04 2013 19:00:00 GMT+0100 (Central European Standard Time). How to subtract timezoneOffset from date in this case?

I am using this function to format it:

convertTimestamp = timestamp => {
  var d = new Date(timestamp), // Convert the passed timestamp to milliseconds
    yyyy = d.getFullYear(),
    mm = ("0" + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
    dd = ("0" + d.getDate()).slice(-2), // Add leading 0.
    hh = d.getHours(),
    h = hh,
    min = ("0" + d.getMinutes()).slice(-2), // Add leading 0.
    sec = ("0" + d.getSeconds()).slice(-2), // Add leading 0.
    ampm = "AM",
    time;

  if (hh > 12) {
    h = hh - 12;
    ampm = "PM";
  } else if (hh === 12) {
    h = 12;
    ampm = "PM";
  } else if (hh === 0) {
    h = 12;
  }

  // ie: 2013-02-18, 8:35 AM
  // time = dd + "/" + mm + "/" + yyyy + " " + h + ":" + min + " " + ampm;
  time = yyyy + "/" + mm + "/" + dd + " " + h + ":" + min + ":" + sec + " " + ampm;

  return time;
};
mplungjan
  • 169,008
  • 28
  • 173
  • 236
youngster
  • 195
  • 1
  • 12
  • 2
    remember that, if you only have `1386180000`, you need to multiply by 1000 to get the right date as `new Date(timestamp * 1000)` – balexandre Sep 18 '19 at 11:13
  • what I tried to do is: var date = new Date(timestamp * 1000 ), removeOffset = - date.getTimezoneOffset() / 60 * 3600000; – youngster Sep 18 '19 at 11:25
  • I just read comments and and proposed answers, and made it. Thx. and you are right @mplungjan it is dupicate. thanks for helping. – youngster Sep 18 '19 at 11:32

0 Answers0