Skip to content Skip to sidebar Skip to footer

Date Validation With Javascript

I have a date string in this format - 'DD-MM-YYYY' this validates that successfully: var dateFormat = /(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-\d{4}/ ; if(!startDate.match(dateFor

Solution 1:

First, this is your current date in javascript:

var today = newDate();
var day = today.getDate();
var month = today.getMonth()+1; // Zero indexed

All you need to do, from here, is to compare this with your start date!

Best regards!

check this out maybe it helps to understand the date object.

Solution 2:

Check out date.js, specifically...

http://code.google.com/p/datejs/wiki/APIDocumentation#compare

Compares the first date to the second date and returns an number indication of their relative values. -1 = this is < date. 0 = values are equal. 1 = this is > date.

The isAfter() and the isBefore() methods might be useful for your problem :)

Download the library here:

http://code.google.com/p/datejs/downloads/detail?name=date.js&can=2&q=


Also, its worth mentioning to checkout moment.js. I think the two libraries complement each other.

Solution 3:

You could do this with moment.js pretty easily.

var input = moment(startDate, "DD-MM-YYYY");
if (input < moment()) {
    // before today
} else {
    // after today
}

We're also adding date validation pretty soon. See more info about validation here: https://github.com/timrwood/moment/pull/306

Solution 4:

Something like this should work. Could use some cleanup, but hopefully gets the point across.

var dateFormat = /(0[1-9]|[12][0-9]|3[01])-(0[1-9]|1[012])-(\d{4})/;
var dateMatch = startDate.exec(dateFormat);
var today = newDate();
today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setMilliseconds(0);

if ((newDate(dateMatch[3], dateMatch[2] - 1, dateMatch[1])).getTime() >= today.getTime()) {
    // Date is after or on today
}

Solution 5:

You should check each date getTime() method and compare it. It's plain and simple, you don't need additional frameworks.

Here is an example that parses the dates from the strings, and then compares them:

var todayDate = "10-05-2012";​ // A sample datevar compareDate1 = "10-05-2012";
var compareDate2 = "03-05-2012";
var compareDate3 = "10-07-2012";

compareDates(todayDate, compareDate1);
compareDates(todayDate, compareDate2);
compareDates(todayDate, compareDate3);

functioncompareDates(date1String, date2String) {
    var date1 = parseDate(date1String);
    var date2 = parseDate(date2String);
    if(date1.getTime() > date2.getTime()) {
      alert("First date(" + date1String + ") is older than second date(" + date2String + ").");                
    } elseif(date1.getTime() < date2.getTime()) {
      alert("First date(" + date1String + ") is younger than second date(" + date2String + ").");                
    } else {
      alert("The dates are the same day");   
    }
}

functionparseDate(stringDateParam) {
    var parsedDay = parseInt(stringDateParam.substring(0,2));
    var parsedMonth = parseInt(stringDateParam.substring(3,5))-1;
    var parsedYear = parseInt(stringDateParam.substring(6,10));
    var parsedDate = newDate(parsedYear, parsedMonth, parsedDay, 0 , 0, 0, 0);
    return parsedDate;
}
​

// Output://// First check: The dates are the same day// Second check: First date(10-05-2012) is older than second date(03-05-2012).// Third check: First date(10-05-2012) is younger than second date(10-07-2012).

You probably already have a function that parses string to date object, and you should implement a check similar to the one in function compareDates based on getTime() function.

If you have further questions, leave a comment. Good Luck!

JSFiddle working example: click here

Post a Comment for "Date Validation With Javascript"