0

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

let date_first = rowNode.data[val[0]]; // 16.02.2021 04:20:55
let date_second = rowNode.data[val[1]]; // 16.02.2021 02:45:35
if (date_first == null || date_second == null) {
  date_first = 0;
  date_second = 0;
}
let get_date_first = Date.parse(new Date(date_first)); // NOTHING
let get_date_second = Date.parse(new Date(date_second)); // NOTHING
rowNode.data[unique_id] = (get_date_first - get_date_second) / 1000;
ViqMontana
  • 5,090
  • 3
  • 19
  • 54
  • I made you a snippet. Please add the rowNode object – mplungjan Mar 02 '21 at 16:20
  • `Date.parse(new Date(date_first))` <-- why are you parsing a date object? – epascarello Mar 02 '21 at 16:23
  • See [*Converting a string to a date in JavaScript*](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) and [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Mar 03 '21 at 00:54

3 Answers3

2

Convert the invalid date string on the server or like this

const getDate = str => new Date(str.replace(/(\d{2})\.(\d{2})\.(\d{4})(.*)/,"$2/$1/$3$4"));

const d1 = getDate("16.02.2021 04:20:55")
const d2 = getDate("16.02.2021 02:20:55")
console.log(Math.floor((d1.getTime()-d2.getTime())/1000))
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) It's not a good strategy to parse a string to another string that must be parsed again by the unreliable built–in parser. Far better to parse once and pass the values directly to the Date constructor. – RobG Mar 03 '21 at 00:11
  • I am not using Date.parse and I suggest to fix this on the server – mplungjan Mar 03 '21 at 05:06
  • `new Date(str.replace(/(\d{2})\.(\d{2})\.(\d{4})(.*)/,"$2/$1/$3$4"))` uses the built–in parser as for `Date.parse(…)`. – RobG Mar 03 '21 at 05:13
  • And is given a reliably parseable string, not an ambiguous one. Anyway – mplungjan Mar 03 '21 at 05:15
1

Have you tried different formats for the string? If I had to guess I would think that it doesn't like '16.02.2021 04:20:55' and so it's not creating a date for you. You may have to do some string manipulation to get it to recognize as an appropriate date-time string format. I would mess around with the format such as trying to replace periods with hyphens, or seeing if there is a setting you can specify to say 'Hey I am using EU str formatting'

Here is the page for date.parse from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – mplungjan Mar 02 '21 at 16:28
  • I tried format "yyyy.mm.dd" it's working, but you see my format (( Add this in my code ` let get_date_second = Date.parse( new Date(date_second.replace(/(\d{2})\. (\d{2})\.(\d{4})(.*)/,"$2/$1/$3$4")) );` All working – Паша Харченко Mar 02 '21 at 16:33
1

All working

            let date_first = rowNode.data[val[0]];
            let date_second = rowNode.data[val[1]];
            if (date_first == null || date_second == null) {
                date_first = 0;
                date_second = 0;
            }
            let get_date_first =  Date.parse( new Date(date_first.replace(/(\d{2})\.(\d{2})\.(\d{4})(.*)/,"$2/$1/$3$4")) );
            let get_date_second =  Date.parse( new Date(date_second.replace(/(\d{2})\.(\d{2})\.(\d{4})(.*)/,"$2/$1/$3$4")) );