Home > Web Front-end > JS Tutorial > js gets the date codes before and after the specified date_javascript skills

js gets the date codes before and after the specified date_javascript skills

WBOY
Release: 2016-05-16 17:24:51
Original
1143 people have browsed it
Copy code The code is as follows:

function getmonths(dateday){
/*Get the current date Month*/
var curDate = new Date(dateday);
return curDate.getMonth() 1;
};
function getYears(dateday){
/*Get 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: Since curMonth will be 1 smaller than the actual month, Therefore, 1 needs to be added */
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 yesterday’s start time, the default is 1 day, the default is not to return a short time
day: start time
amount: the number of days apart
isShortTime: whether to display short time
*/
function getBeforeDay(day,amount,isShortTime){
if(Date.parse(day) != Date.parse(day )){
return false;
}
var days = new Date(new Date(day)-1000*60*60*24);
if(amount){
var number = parseInt(amount);
if(number && number > 0 && !isNaN(number)){
days = new Date(new Date(day)-1000*60*60*24*number);
}
}
return dateFormat(days,isShortTime);
}
/*
Get the start time tomorrow, the default is 1 day, the default is not to return a short time
day: start time
amount: number of days apart
isShortTime: whether to display short time
*/
function getAfterDay(day,amount,isShortTime){
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);
}
/*
Format time, the default is Do not return short time
day : Date
isShortTime : Whether to display 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template