从时间戳减去时区

时间:2019-09-18 11:10:08

标签: javascript

我有时间表示为:1386180000,在javascript中,它添加了时区,如:Wed Dec 04 2013 19:00:00 GMT + 0100(中欧标准时间)。在这种情况下,如何从日期中减去timezoneOffset?

我正在使用此功能对其进行格式化:

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;
};

0 个答案:

没有答案