0

For now I'm using this long method to convert user input in format: DD.MM.YY HH:mm to milliseconds.

let day = date.substring(0, 2);
let mth = date.substring(3, 5);
let yr = date.substring(6, 10);
let hr = date.substring(11, 13);
let min = date.substring(14, 16);

let d = yr + "-" + mth + "-" + day + "T" + hr + ":" + min + ":00.000+02:00";
d = new Date(d).getTime();

Is there any easier, preferable way to do this? I've read about ISO dates and other norms but I haven't found anything that includes both day/month/year and hours/minutes without unnecessary milliseconds and time zones.

I'm not expert in js dates so be understanding. Thank you in advance.

  • `Date` object in javascript is already based in milliseconds. So all you need is `date.now()`. ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – mardubbles Apr 15 '22 at 12:21
  • I think your question was answered in [this](https://stackoverflow.com/questions/9229213/convert-iso-date-to-milliseconds-in-javascript) thread – PlainXYZ Apr 15 '22 at 12:22
  • Yes. Given a timestamp like "2022.04.15 13:52" you could use `let [yr, mth, day, hr, min] = dateString.split(/\D/); new Date(yr, mth-1, day, hr, min).getTime();`. ;-) – RobG Apr 16 '22 at 02:45

1 Answers1

0

You can try Date.parse which parses a date string. The MDN website has a full write-up on it at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Mifo
  • 563
  • 6
  • 6