Moment.JS:时间不基于计算机时间



我有一个日期列表。瞬间。

我的问题是,这是在我的计算机时间/日期中衡量的。因此,如果我改变时间或某人在另一个时区中,措辞是错误的。

是否有使用自动中心时间和/或时区的解决方案?

这是codepen:https://codepen.io/anon/pen/qrwdvj

html:

<div class="timetracker">05.05.2019 17:45</div>
<div class="timetracker">06.05.2019 17:45</div>
<div class="timetracker">07.05.2019 17:45</div>
<div class="timetracker">08.05.2019 17:45</div>
<div class="timetracker">09.05.2019 17:45</div>

JS

$(document).ready(function() {
  $('.timetracker').html((index, html) => {
    let date = moment(html, "DD.MM.YYYY HH:mm", true),
      now = moment(),
      today = moment().endOf('day'),
      today1 = moment().add(1, 'day').endOf('day'),
      today2 = moment().add(2, 'day').endOf('day'),
      today3 = moment().add(3, 'day').endOf('day'),
      minutes = now.diff(date, "minutes"),
      hours = now.diff(date, "hours"),
      days = now.diff(date, "days"),
      weeks = now.diff(date, "weeks"),
      result = "";
      if (minutes >= 0 && minutes <= 110) {
        result = "right now";
      }
      else if (minutes > 110) {
        result = "past";
      }
      else if (date < today ) {
        result = "today";
      }
      else if (date < today1) {
        result = "future";
      }
    return result;
  });
});

解决方案:

$(document).ready(function() {
  $('.timetracker').html((index, html) => {
    let date = moment(moment.utc(html, "DD.MM.YYYY HH:mm", true).subtract(moment.duration("02:00:00"))).local(),
      now = moment(),
      minutes = now.diff(date, "minutes");
    return minutes;
  });
});

现在将UTC转换为CET时间(减去2H(,然后将其基于当地时区。

https://codepen.io/anon/pen/vwewor

相关内容

最新更新