Js get the date code before and after a specified date _ javascript skills

Source: Internet
Author: User
Js gets the date before and after the specified date, which is more practical in actual application. The following describes the specific implementation process for you. If you need it, refer The Code is as follows:


Function getmonths (dateday ){
/* Obtain the month of the current date */
Var curDate = new Date (dateday );
Return curDate. getMonth () + 1;
};
Function getYears (dateday ){
/* Obtain the year of the current date */
Var curDate = new Date (dateday );
Return curDate. getFullYear ();
};
Function getCountDays (dateday ){
/* Conversion time */
Var curDate = new Date (dateday );
/* Get the current month */
Var curMonth = curDate. getMonth ();
/* Generate the actual month: Because curMonth is 1 less than the actual month, you need to add 1 */
CurDate. setMonth (curMonth + 1 );
/* Set the date to 0 */
CurDate. setDate (0 );
/* Return the number of days in the current month */
Return curDate. getDate ();
};
/*
Get the start time of yesterday. The default value is 1 day. The default value is not to return a short time.
Day: Start Time
Amount: the number of days
IsShortTime: whether to display a short time
*/
Function getBeforeDay (day, amount, isShortTime ){
If (Date. parse (day )! = Date. parse (day )){
Return false;
}
Var days = new Date (day)-1000*60*60*24 );
If (amount ){
Var number = parseInt (amount );
If (number & number> 0 &&! IsNaN (number )){
Days = new Date (day)-1000*60*60*24 * number );
}
}
Return dateFormat (days, isShortTime );
}
/*
Get the start time of tomorrow. The default value is 1 day. The default value is not to return a short time.
Day: Start Time
Amount: the number of days
IsShortTime: whether to display a short time
*/
Function getAfterDay (day, amount, istertime ){
If (Date. parse (day )! = Date. parse (day )){
Return false;
}
Day = new Date (day );
Var v = day. valueOf ();
Var days = new Date (v + 86400000 ));
If (amount ){
Var number = parseInt (amount );
If (number & number> 0 &&! IsNaN (number )){
Days = new Date (v + 86400000 * number ));
}
}
Return dateFormat (days, isShortTime );
}
/*
Formatting time. By default, no short time is returned.
Day: Date
IsShortTime: whether to display a short time
*/
Function dateFormat (day, isShortTime ){
If (Date. parse (day )! = Date. parse (day )){
Return false;
}
Var days = new Date (day );
If (isShortTime & isShortTime = true ){
Var hours = days. getHours () <10? "0" + days. getHours (): days. getHours ();
Var minutes = days. getMinutes () <10? "0" + days. getMinutes (): days. getMinutes ();
Var seconds = days. getSeconds () <10? "0" + days. getSeconds (): days. getSeconds ();
Return days. getFullYear () + "-" + (days. getMonth () + 1) + "-" + days. getDate () + "" + hours + ":" + minutes + ":" + seconds;
} Else {
Return days. getFullYear () + "-" + (days. getMonth () + 1) + "-" + days. getDate ();
}
}

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.