I have a form with mathematical operations, the user has the right to select two dates and perform a subtraction operation. To do this, I need to translate each date into seconds and read the seconds.
I cant get seconds with date format like this
JavaScript
x
9
1
let date_first = rowNode.data[val[0]]; // 16.02.2021 04:20:55
2
let date_second = rowNode.data[val[1]]; // 16.02.2021 02:45:35
3
if (date_first == null || date_second == null) {
4
date_first = 0;
5
date_second = 0;
6
}
7
let get_date_first = Date.parse(new Date(date_first)); // NOTHING
8
let get_date_second = Date.parse(new Date(date_second)); // NOTHING
9
rowNode.data[unique_id] = (get_date_first - get_date_second) / 1000;
Advertisement
Answer
Convert the invalid date string on the server or like this
JavaScript
1
5
1
const getDate = str => new Date(str.replace(/(d{2}).(d{2}).(d{4})(.*)/,"$2/$1/$3$4"));
2
3
const d1 = getDate("16.02.2021 04:20:55")
4
const d2 = getDate("16.02.2021 02:20:55")
5
console.log(Math.floor((d1.getTime()-d2.getTime())/1000))