Javascript to compare two dates, starting with strings, start <= end

I get two lines formed as (Brazilian format): "DD / MM / YYYY", I need to compare both. Since the first field is the beginning, and the last is the end,

My check begins <= end

Date.new (start) generates an "invalid date" even in ISO!

+5
source share
6 answers

Do not use Date.new. Use new Date(). Due to the format of your date string, I would recommend grabbing each field separately and passing them to the constructor:

var startYear = parseInt(document.getElementById('startYear'), 10);
var startMonth = parseInt(document.getElementById('startMonth'), 10) - 1; // as per Residuum comment
var startDay = parseInt(document.getElementById('startDay'), 10);
var start = new Date(startYear, startMonth, startDay);

... , , . , Date .

, , :

function isValid(start, end) {
    return start.getTime() < end.getTime();
}
+8

, datejs, parseExact(dateStr, format).

+5

, , , dd/mm/yyyy

today = "23/02/1001";
dateComponents = today.split("/");
date = new Date(dateComponents[2], dateComponents[1] - 1, dateComponents[0]);

Datejs, .

+2

"":

function is_valid (start , end) {
     return start.split('/').reverse().join('') <= end.split('/').reverse().join('') ;
}

, , , .

: , , , / 10 .

+1
+1

Javascript:

dateobject = new Date(); // returns date of current time stamp
                         // on the browser
dateobject = new Date("Month Day, Year Hours:Minutes:Seconds");
dateobject = new Date(Year, Month, Day);
dateobject = new Date(Year, Month, Day, Hours, Minutes, Seconds);
dateobject = new Date(Milliseconds);

, , new Date(Year, Month, Day); .

EDIT: : Month Javascript, 1 2010 new Date(2010, 0, 1), 31 2011 - new Date(2010, 11, 31).

0

All Articles