0

I have two strings which represent the date/time in Greenwich Mean Time (GMT) which I wish to convert to their Coordinated Universal Time (or UTC) equivalent.

  1. 2019-11-01 09:58
  2. 2019-10-01 08:58

For the first string the time should remain the same when converted to UTC, as Daylight saving time (DST) is not used in November.

Whereas the time from the second string should change to 1 hour earlier when converted to UTC, as DST is used in early October.

I would like to convert both of these strings with moment.js and moment-timezone.js

So far, I've tried the below without success, any suggestions?

var format = 'D MMMM Y HH:mm';

console.log(
  "First momement() call should return value with 09:58 and next one with 08:58': ",'\n',
  moment("2019-11-01 09:58", 'YYYY-MM-DD HH:mm').tz('GMT').utc().format(format),'\n',
  moment("2019-10-01 09:58", 'YYYY-MM-DD HH:mm').tz('GMT').utc().format(format)
);

console.log(
  "First momement() call should return value with 09:58 and next one with 08:58': ",'\n',
  moment.tz("2019-11-01 09:58", 'YYYY-MM-DD HH:mm',"GMT").utc().format(format),'\n',
  moment.tz("2019-10-01 09:58", 'YYYY-MM-DD HH:mm',"GMT").utc().format(format)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.5/moment-timezone-with-data-2010-2020.min.js"></script>
Holly
  • 7,462
  • 23
  • 86
  • 140
  • [There's no meaningful difference between the two](https://www.google.com/search?sxsrf=ACYBGNQ5EfJhsjYaZ6MxdBlfDGi8RStzaQ%3A1572291036841&source=hp&ei=3EG3XdryMIbB-wTg-qTYAg&q=difference+between+utc+and+gmt&btnK=Google+Search&oq=measure+run+time+&gs_l=psy-ab.3.0.0i20i263j0l3j0i22i30l6.482.3766..4919...1.0..0.201.2147.1j16j1......0....1..gws-wiz.......0i131j35i39j0i67j0i10.mnaSR3X-FR8). Neither change for DST: they are always the same time. One is a timezone, and the other is a standard, but they refer to the same clock time. – Jared Smith Oct 31 '19 at 14:41

1 Answers1

0

Ahh, I found out what it was. Instead of using GMT to convert it, I needed to use the timezone location/place.

var format = 'D MMMM Y HH:mm';

console.log(
  "First momement() call should return value with 09:58 and next one with 08:58': ",'\n',
  moment.tz("2019-11-01 09:58", 'YYYY-MM-DD HH:mm','Europe/London').utc().format(format),'\n',
  moment.tz("2019-10-01 09:58", 'YYYY-MM-DD HH:mm','Europe/London').utc().format(format)
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.5/moment-timezone-with-data-2010-2020.min.js"></script>
Holly
  • 7,462
  • 23
  • 86
  • 140