0

I have a list of dates/times. With moment.js they were converted to wordings like past, today, future and so on.

My problem is, that this is measured on the time/date of my computer. So if I change my time or somebody is in an other timezone, the wordings were wrong.

Is there any solution to use a automated central time and/or timezones?

Here is the 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;
  });

});
Maximilian Neuse
  • 153
  • 1
  • 5
  • 15
  • Possible duplicate of [How to create time in a specific time zone with moment.js](https://stackoverflow.com/questions/18448347/how-to-create-time-in-a-specific-time-zone-with-moment-js) – Heretic Monkey May 06 '19 at 15:56
  • Unfortunalety no, because the time should be converted automatically based on the users timezone – Maximilian Neuse May 07 '19 at 07:24
  • I think the solution is to change the date UTC and change to code: let date = moment(moment.utc(html, "DD.MM.YYYY HH:mm", true)).local(), - update codepen: https://codepen.io/anon/pen/QRwdvj – Maximilian Neuse May 07 '19 at 07:35

1 Answers1

0

The solution:

$(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;
  });

});

This now converts utc to cet time (subtracting 2h) - and base it on the locals timezone.

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

Maximilian Neuse
  • 153
  • 1
  • 5
  • 15