JS Verify that two dates are the same month, and calculate the number of days difference

Source: Internet
Author: User


/**
* Convert dates to YYYY-MM format.
* Help verify that two times are in the same year of the month
* @param datestr
*/
function Formatyearmonth (b_date) {
var b_date_year = B_date.getyear ();
var b_date_month = B_date.getmonth () +1;

if (B_date_month < 10) {
B_date_month = "0" + b_date_month;
}
return b_date_year + "" + b_date_month;
}

function Getdatesub (date_b,date_e) {

The following must have, otherwise it will be but a Nan
var b_date = new Date (Date.parse (Date_b.replace (/-/g, '/')));
var e_date = new Date (Date.parse (Date_e.replace (/-/g, '/')));

var b_year_month = Formatyearmonth (b_date);
var e_year_month = Formatyearmonth (e_date);

if (b_year_month! = e_year_month) {
Alert ("Please keep the start and end dates of the stops within the same month.") ");
return-1;
}
Return parseint (Math.Abs ((e_date-b_date)/86400000) + 1;
}

/**
* Verify that the start date to the end date must be the same as one months
*/
function Validatedate () {
var date_b = form. Begin_date.value;
var date_e = form. End_date.value;
Verify date format
if (!isdate (Date_b)) {
Alert (the Stop start date is not a valid date yyyy-mm-dd. ");
return false;
}
if (!isdate (date_e)) {
Alert (the stop end date is not a valid date yyyy-mm-dd. ");
return False
}

if (Date_b > Date_e) {
Alert ("The starting time of the stop start date cannot be later than the end Time");
return false;
}

Start calculating two date difference values (1-31 is 31 days, not 30 days. )
var datesub = getdatesub (date_b,date_e);
if (datesub = =-1) {
return false;
}

Form. Tc_days.value = datesub;

return true;

}

JS Verify that two dates are the same month, and calculate the number of days difference

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.