I use mysql library for node.js. The problem is that not all dates are inserted, which is very incomprehensible how this generally works.

I’ll say right away that it’s written on the library page on the github that by creating an object through new Date in js, you can insert data.

Code:

 connection.connect(); var date1 = new Date('04.07.2016'); var date2 = new Date('23.06.2013'); var queryObject = { date1: date1, date2: date2 }; connection.query('INSERT INTO test SET ?', queryObject, function (error, results, fields) { if (error) throw error; }); connection.end(); 

What happens in the database:

 mysql> select * from test; +----+------------+-------+ | id | date1 | date2 | +----+------------+-------+ | 4 | 2016-04-07 | NULL | +----+------------+-------+ 

Why is the date 07/04/2016 inserted, and the second is not?

  • And if the second date is 03/06/2013, for example, will it be inserted? (: - Suvitruf ♦
  • @Suvitruf yes, stuck ... but how? Why? - Alexxosipov
  • one
    I suspect that the problem is in the format of dates. Your 23.06.2013 he is trying to parse the format mm:dd:yyyy and that does not work. Try writing the month instead of the day in the first place. - Suvitruf ♦
  • @Alexxosipov xkcd.ru/i/1179_v2.png - andreymal
  • @Alexxosipov yes, stuck ... but how? Why? The handler accepts the first digit as the month 04.07.2016 - this is April 7, 2016, and the month with number 23 does not exist. - Alex

1 answer 1

Probably a problem with the date format. I would suggest working with dates with the library of the moment :

 var date1Original = '04.07.2016'; var date2Original = '23.06.2013'; const date1 = moment(date1Original , "DD.MM.YYYY").toDate(); const date2 = moment(date2Original , "DD.MM.YYYY").toDate(); var queryObject = { date1: date1, date2: date2 }; 

In this case, exactly the problems should not be.

In fact, I don’t know what will happen after new Date('04.07.2016') , since the input string must meet the specification . And it says that the string must comply with ISO 8601: YYYY-MM-DDTHH:mm:ss.sssZ .

Or you can transfer the day, month and year in separate fields, as in the examples docks :

 new Date(year, month, date, hours, minutes, seconds, milliseconds); 
  • “What will happen in the end” - I didn’t check it in Nodezhs, but my firefox returned a special object “Invalid Date” - andreymal
  • ... but I checked it in the node - it was back there 2016-04-06T21:00:00.000Z , interestingly - andreymal
  • one
    @andreymal therefore it is better to use an explicit constructor using new Date(year, month, date, hours, minutes, seconds, milliseconds); or special libraries like the moment . - Suvitruf ♦