-2

Does anyone have an idea how to convert time into timestamp?

I tried to use .getTime() but unfortunately it changes the day with the month

const date = new Date('01-02-2003 01:02:03');
console.log(date.getTime());

It looks like a conversation to the US Timezone. I expect: dd/mm/yyyy hh:mm:ss

tadman
  • 208,517
  • 23
  • 234
  • 262
Vejmal
  • 1
  • 2
  • `01-02-2003` is January 2nd or Februrary 1st? Does `Date.parse()` do the right thing? The output format will likely be dictated by your locale. – tadman Jun 06 '23 at 21:27

1 Answers1

0

I have written a small snippet of code that might help you

const getTimestamp = (dateString) => {
    const [date, time] = dateString.split(' ');
    const [day, month, year] = date.split('-');
    const [hh, mm, ss] = time.split(':');

    const date = new Date(year, month-1, day, hh, mm, ss);

    return date.getTime();
}

It is based in this overload of Date() constructor

new Date(year, monthIndex, day, hours, minutes, seconds)