我有一个日期/时间列表。借助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;
});
});
答案 0 :(得分:0)
解决方案:
$(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)-并以当地时区为基础。