3

I must convert a date time, given its time zone, year, month, day, hours and minutes to an ISO string.

For example, given the following parameters:

{
 timeZone: 'Europe/Paris',
 year: 2020,
 month: 11,
 day: 18,
 hours: 14,
 minutes: 44,
}

I want to build the ISO string corresponding: 2020-11-18T13:44:00.000Z (notice the hour shift there)

You can do this the other way around pretty easily, either with the Intl.DateTimeFormat or the toLocaleDateString/toLocaleTimeString methods, but this way I can't find a proper solution... If I overlooked any source of information, please let me know.

EDIT (see comments):

The perk of using a time zone and not a GMT string, such a 'GMT +01:00', is that I won't have to handle time changes. As you may know, the time zone 'Europe/Paris' is 'GMT +01:00' in the winter but 'GMT +022:00' in the summer... And I can't find a proper way to map the timezone to any UTC offset

Thank you in advance for your help

SOLUTION:

As suggested below, using Luxon we can do

const timeObject = { day, month, year, hours, minutes, zone: timeZone };
const date = DateTime.fromObject(timeObject).toUTC().toString();

Matt also suggested the for now experimental Temporal feature.

Justin Grant
  • 44,807
  • 15
  • 124
  • 208
rguerin
  • 2,068
  • 14
  • 29
  • What about all the UTC methods on the Date prototype? And `.toUTCString()`? – Pointy Nov 18 '20 at 13:55
  • I cannot use this methods as i cannot set the timezone. The server executing this code is to UTC 0, so any "new Date()" instructions will return an unconverted date time. I will get a unshifted ISO string (eg: 2020-11-18T14:44:00.000Z instead of 2020-11-18T13:44:00.000Z) – rguerin Nov 18 '20 at 13:58
  • Your question says that you have the time zone information ("Europe/Paris"). – Pointy Nov 18 '20 at 14:00
  • If you can map the string time zone information to an offset, you can build your own ISO string that includes the offset. Then you can extract the constructed UTC version. – Pointy Nov 18 '20 at 14:03
  • That is exactly my point. The perk of using a timeZone and not a GMT string (such a GMT +01:00) is that I won't have to handle time changes. As you may know, the timeZone 'Europe/Paris' is GMT +1 in the winter but GMT +2 in the summer... And I can't find a proper way to map the timezone to any offset – rguerin Nov 18 '20 at 14:07
  • Well you could look into using Moment, which allows for a locale database that's extendable if necessary. – Pointy Nov 18 '20 at 14:11
  • I'm trying to avoid using Moment.js as it starts being deprecated... Thank you for the help anyways! – rguerin Nov 18 '20 at 14:19

3 Answers3

2

When Temporal is available in your browser engine (or if you use a polyfill) then just do this:

Temporal.ZonedDateTime.from({
  timeZone: 'Europe/Paris',
  year: 2020,
  month: 11,
  day: 18,
  hour: 14,
  minute: 44,
}).toInstant().toString();
// => '2020-11-18T13:44:00Z'
Justin Grant
  • 44,807
  • 15
  • 124
  • 208
1

Using Luxon (the successor to Moment).

// your input object
const o = {
  timeZone: 'Europe/Paris',
  year: 2020,
  month: 11,
  day: 18,
  hours: 14,
  minutes: 44,
};

// create a Luxon DateTime
const dt = luxon.DateTime.fromObject({
  year: o.year,
  month: o.month,
  day: o.day,
  hour: o.hours,
  minute: o.minutes,
  zone: o.timeZone
});

// convert to UTC and format as ISO
const s = dt.toUTC().toString();

console.log(s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js"></script>

Of course, you could simplify if your input object used the same field names as Luxon needs.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    Note, this scenario will be better handled by [Temporal](https://tc39.es/proposal-temporal/docs/index.html) if and when it is eventually incorporated into the JavaScript language standard. – Matt Johnson-Pint Nov 18 '20 at 19:49
  • Thanks a lot! It is what I ended to do, in two lines... I've updated my question. Temporal looks very interesting to! Thank you again for the info – rguerin Nov 19 '20 at 09:17
-1

here is my answer:

    var year = 2020;
    var month = 11;
    var day = 18;
    var hours = 14;
    var minutes = 44;
    var string = years + month + day + hours + minutes;

if you want to display the string on output:

    var year = 2020;
    var month = 11;
    var day = 18;
    var hours = 14;
    var minutes = 44;
    var string = years + month + day + hours + minutes;
    document.getElementById("demo").innerHTML = "time in string" + 
    string
  • This does not answer the question, I want the ISO string to be converted to UTC time. You do not handle the time zone here. – rguerin Nov 18 '20 at 14:18