0

Hello i am new to javascript , i tried a lot i cant get this format yyyy-mm-ddT00:00:00, i want to get this format in java script to compare it with other dates in an array and then get the next valid date that will come .

also if some one can show me a function on how to compare and get the closest greater date than today .

datesarray = ['2017-12-09T00:00:00' ,'2017-12-13T00:00:00' ,'2017-12-02T00:00:00' ,'2017-12-16T00:00:00']

let diffDate = Infinity;
let now = Date.now;

for (let i = 0; i < datesarray.length; i++) {

            let closest = datesarray[0]; 
            if (closest > now && closest < diffDate)
            {
                diffDate = closest;
            }

}

this is what i tried but its never getting into the if statement . PS : i have other dates in an array but i did not show them here .

taysonn
  • 43
  • 2
  • 13
  • `let closest = 2017-12-09T00:00:00` = boom, syntax error (quotes missing). Don't reinvent the wheel and use [MomentJS](https://momentjs.com)... Something like `moment().format("yyyy-mm-ddT00:00:00")` – Jeremy Thille Oct 19 '17 at 12:35
  • check the edit please . @JeremyThille – taysonn Oct 19 '17 at 12:37
  • Checked, so? Nothing has changed, still dates with no quotes. `Uncaught SyntaxError: Invalid or unexpected token` – Jeremy Thille Oct 19 '17 at 12:39
  • You can check this link out: https://stackoverflow.com/questions/23593052/format-javascript-date-to-yyyy-mm-dd or this one: https://stackoverflow.com/questions/12409299/how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-i – Nino9612 Oct 19 '17 at 12:40
  • the isostring fixed it but now the other part of the question :) @Nino9612 – taysonn Oct 19 '17 at 12:45

3 Answers3

1

I think you have two separate questions here, to get an iso formatted date string you would use Date.prototype.toISOString(), but to compare dates and find the closest you would need to turn those strings into date objects and compare

let datesArray = ['2017-12-09T00:00:00' ,'2017-12-13T00:00:00' ,'2017-12-02T00:00:00' ,'2017-12-16T00:00:00'];
let diffDate = Infinity;
let now = Date.now();
    
for (let i = 0, len = datesArray.length; i < len; i++) {
  let closest = new Date(datesArray[i]); 
  if (closest > now && closest < diffDate) {
    diffDate = closest;
  }
}
console.log(diffDate.toISOString());

Edit 1

In answer to your question, I can't duplicate your undefined problem using the code above, and to illustrate that diffDate is set correctly I moved it outside of the loop, if you run the snippet you should see the closest date print.

That being said, this might be a more concise way to handle your problem as long as modern JS syntax is workable for your issue.

const datesArray = ['2017-12-09T00:00:00' ,'2017-12-13T00:00:00' ,'2017-12-02T00:00:00' ,'2017-12-16T00:00:00'];
const dateCompare = (closest, current) => {
  const t = new Date(current);
  return (t > Date.now() && t < closest) ? t : closest;
}
let closest = datesArray.reduce(dateCompare, Infinity)
console.log(closest.toISOString());

Edit 2

To handle timezone precision problems if possible you want your date array to have dates in iso format 'yyyy-mm-ddThh:mm:ssZ' | '2017-12-02T00:00:00Z' if you can't edit your date array format you should make sure to append that 'Z' before creating your date: new Date(current + 'Z');

D Lowther
  • 1,541
  • 1
  • 6
  • 15
  • the best answer until now but the variable diffDate is not showing value , its undefined . is there any other thing i can use than infinity – taysonn Oct 19 '17 at 12:53
  • i dont see this date in the array . 2017-12-01T23:00:00.000Z ??!! when i run the snipp code – taysonn Oct 19 '17 at 13:33
  • that date is also not in the example array I copied? *Edit* Wait I think I get what you are saying. Do you happen to be at UTC - 1 for your timezone? – D Lowther Oct 19 '17 at 13:59
0

For your first question, use the ISOString() function of the Date object.

https://www.w3schools.com/jsref/jsref_toisostring.asp

For the next one, either create date object of each time, and then compare, or if you need to support equality check, convert all to milliseconds from the date object.

thedarkone
  • 372
  • 2
  • 10
0

Use the Date() object. There are many different ways to enter the date. One is what you are using.

var dateArray = [new Date('2017-12-09T00:00:00'), new Date(2017, 11, 13), new Date(2017, 11, 2), new Date(2017, 11, 16)];
var today = new Date();
var closestDate = new Date(5000, 0, 0); 
for (var i = 0; i < dateArray.length; i++) {
    var curr = dateArray[i];
    if(curr > today && curr < closestDate) {
        closestDate = new Date(curr);
    }
}
console.log(closestDate); // Prints Sat Dec 02 2017 00:00:00 GMT-0500 as of 10/19/2017
Jimenemex
  • 2,836
  • 2
  • 13
  • 39