I
I
Igor2019-07-08 22:14:29
JavaScript
Igor, 2019-07-08 22:14:29

How to convert date as string to UTC (timestamp)?

How to convert date as string to UTC (timestamp)?
That is, the date "07/15/2019" into an integer.
So that I could create an object of the DateTime class based on this number in php
As I understand it, in JS this question is quite relevant.
What if the order of "2019.07.15" changes

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor, 2019-07-09
@IgorPI

Decision!

export function StrDateToUnix(strDate) {
    let year, month, day
    [year, month, day]  = strDate.split("-")
    console.log(year, month, day)
    return Math.round(new Date(year, month-1, day).getTime()/1000)
}

Special thanks to DanKud

H
hzzzzl, 2019-07-08
@hzzzzl

d = '15.07.2019';
[day, month, year] = d.split('.')
new Date(year, month - 1, day).toUTCString()
// "Sun, 14 Jul 2019 21:00:00 GMT"

d = '15.07.2019';
[day, month, year] = d.split('.')
new Date(year, month - 1, day).getTime()
// 1563138000000

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question