0

I have specific DateTime values in this format 'YYYY-MM-DDThh:mm:ss.ms' (e.g. '2022-05-10T13:44:00.0000000') and I need to convert them to local DateTime (which is UTC+2 for me) without changing the format (so '2022-05-10T15:44:00.0000000' is the desired result (even better would be without the milliseconds but that would just be the icing on the cake)).

I've searched far and wide but every alleged solution I find either changes the format or doesn't change the time at all.

This is what I have right now, it successfully converts the time to local time but by running it through .toISOString() to get the original format back it converts it back to UTC time.

//Input: event.start.dateTime = '2022-05-10T13:44:00.0000000'

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
document.getElementById('ev-start').value = 
startDateTime.toISOString().slice(0,16);

//Output: '2022-05-10T13:44:00.000Z'
Yami
  • 39
  • 5
  • The date is stored internally as ticks from the epoch with 0 timezone offset. When you display it with the .toString___ functions, it displays with your local time. Also, the `event.start.dateTime` you have does not have a timezone specification, so it is parsed as your local time. If your starting time is at +00:00, then add that specification and you don't need to do any math -- just use the toISOString() and trim it if you must. – Jasen May 10 '22 at 18:36
  • See here https://stackoverflow.com/q/3552461/2030565 for more formatting options. – Jasen May 10 '22 at 18:37
  • I looked through all the formatting options in that link but still couldn't find a good solution, the best solution I found was doing the formatting myself - so I did that. So thank you! – Yami May 11 '22 at 11:20

2 Answers2

0

I couldn't find a clean and satisfying solution so I decided to just to the formatting myself. Here's what I ended up with:

Input: '2022-05-10T13:44:00.0000000'

let startDateTime = new Date(event.start.dateTime);
startDateTime.setMinutes(startDateTime.getMinutes() - 
startDateTime.getTimezoneOffset());
let localStartDateTime = startDateTime.getFullYear() + "-" + 
       (startDateTime.getMonth() + 1).toString().padStart(2, '0') +
       "-" + startDateTime.getDate().toString().padStart(2, '0') + 
       "T" + startDateTime.getHours().toString().padStart(2, '0') + 
       ":" + startDateTime.getMinutes().toString().padStart(2, '0') 
       + ":" +
       startDateTime.getSeconds().toString().padStart(2, '0');
       document.getElementById('ev-start').value = 
       localStartDateTime;

Output: '2022-05-10T15:44:00'

Yami
  • 39
  • 5
0

Hope this helps

const startDateTime = new Date('2022-05-10T13:44:00.0000000');

const outputDateTime = new Date(startDateTime.toString()).toISOString().slice(0, 19);

//document.getElementById('ev-start').value = outputDateTime

console.log(outputDateTime);
  • It doesn't, since (as I stated in my question) function toISOString() converts the time back to UTC time, but thanks anyway; the solution I posted as a comment works just fine though – Yami May 14 '22 at 11:47