0

I am iterating through an array of elements in php and accessing each element through javascript. I want to compare dates, but in order to do that I have to take out the information I need to do a new Date() object. This is what the information looks like in php:

"0": {
.
.
.
"dateInfo": {
  "date": "2021-11-19 00:00:000000",
  "timezone_type": 3,
  "timezone: "UTC"
  },
.
.
.
},

I am accessing these from an array in javascript by doing:

array["0"]["dateInfo"]

I tried to take the just the year month and date and make that into a new Date() object by doing this:

var parts = array["0"]["dateInfo"]["date"].split('-');
var dateToComp = new Date(parts[0], parts[1], parts[2]);

But when I did a console.log(dateToComp); it said it was an invalid date. My end goal to to compare this date with todays date to get something in the fashion:

var today = new Date()
var parts = array["0"]["dateInfo"]["date"].split('-');
var dateToComp = new Date(parts[0], parts[1], parts[2]);

if(dateToComp < today)
{
   return 0; // in the past
}
else
{
   return 1; // same day or future day
}

I hope what I am trying to say makes sense and any help would be much appreciated. Thank you.

  • You may want to double-check your values in javascript. The var_dump in PHP may not be what you're getting in javascript. `console.log(array["0"]["dateInfo"]);` to see what that contains. – aynber Dec 15 '21 at 20:32
  • If the date does contain what you think, then `parts[2]` won't. It will be `19 00:00:000000` – aynber Dec 15 '21 at 20:33
  • `$newDate = date( 'm/d/y g:i A', strtotime(array["0"]["dateInfo"]["date"]));` – Zak Dec 15 '21 at 20:37

3 Answers3

1

But when I did a console.log(dateToComp); it said it was an invalid date.

Because parts[2] is "19 00:00:000000", which is an invalid date component. Instead split on any non–digit character \D. Also, you need to subtract 1 from the month.

E.g.

let date = '2021-11-19 00:00:000000';
// Split on any non–digit character
let [y, m, d] = date.split(/\D/);
// Months are zero indexed
// Treat input as local
console.log(new Date(y, m-1, d).toString());
// Treat input as UTC
console.log(new Date(Date.UTC(y, m-1, d)).toISOString());
RobG
  • 142,382
  • 31
  • 172
  • 209
0
var dateToComp = new Date(parts[0], parts[1], parts[2].split(' ')[0]);
  • 3
    @CinCout, please refrain from reviewing answers on technologies you're not familiar with. This answer is an honest attempt at answering the question. However, it is of low quality because it is a code-only answer. [SO]'s quality standards suggest code in answers should be accompanied by a brief explanation on why/how the code solves the problem. – tao Dec 17 '21 at 01:42
-1
new Date(array["0"]["dateInfo"]["date"])

should work

  • 1
    The OP format is not supported by ECMA-262 so parsing is implementation dependent. `new Date("2021-11-19 00:00:000000")` produces an invalid Date in Safari at least, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Dec 16 '21 at 02:55
  • This worked perfectly – Alexander Wood Dec 17 '21 at 04:58