JS获取系统的指定定年月日

2023-05-26,,

 /**
* 获取系统当前时间
*/
function getNowYearMouth(){
var date=new Date;
var nowYearMouth=date.getMonth()+1;
var year=date.getFullYear();
var day = date.getDate();
nowYearMouth =(nowYearMouth<10 ? "0"+nowYearMouth:nowYearMouth); //获取当前时间----月份
var nowYearMouthCur = (year.toString()+"-"+nowYearMouth.toString()); //获取当前时间----年月
var beforsixYearMouthday= getBeforeYearMouthDay();
return nowYearMouthCur;
} /**
* 获取系统当前年月日
*/
function getNowYearMouthDay(){
var date=new Date;
var nowYearMouth=date.getMonth()+1;
var year=date.getFullYear();
var day = date.getDate();
nowYearMouth =(nowYearMouth<10 ? "0"+nowYearMouth:nowYearMouth); //获取当前时间----月份
day=(day<10?"0"+day:day);
var nowYearMouthDay = (year.toString()+"-"+nowYearMouth.toString()+"-"+day.toString());//获取系统当前的年月日
return nowYearMouthDay;
} /**
* 获取系统前六个月时间--年月
*/
function getbeforeYearMouth() {
var date=new Date;
// date = new Date( date - 86400000 * 30 * 10 );
var month=date.getMonth()+1;
var year=date.getFullYear();
for (var i=0;i<6;i++)
{
if(month<=0)
{
month = month+12;
year--;
}
var tempmonth =(month<10 ? "0"+month:month);
var mydate = year.toString()+"-"+tempmonth.toString();
month--;
}
return mydate;
}
/**
* 获取系统前六个月的年月日
*/
function getBeforeYearMouthDay(){
var tempmonth;
var date=new Date;
var month=date.getMonth()+1;
var year=date.getFullYear();
var day = date.getDate();
for (var i=0;i<=12;i++){
if(month<=0) {
month = month+12;
year--;
}
tempmonth =(month<10 ? "0"+month:month);
month--;
}
if((year%100!=0)&(year%400==0)&&(year%4==0)){//是闰年的情况下
if(tempmonth==2){ //若果是2月份
if(day>29){
tempmonth = tempmonth+1;
}else{
tempmonth = tempmonth;
}
}else{//不是2月份的情况
tempmonth = tempmonth;
}
}else{//不是闰年的情况下
if(tempmonth==2){ //若果是2月份
if(day>28){
tempmonth = tempmonth+1;
}else{
tempmonth = tempmonth;
}
}else{//不是2月份的情况
tempmonth = tempmonth;
}
}
if(day<10){
day="0"+day;
}
var beforeYearMouthDay = year.toString()+"-"+tempmonth.toString()+"-"+day.toString();
return beforeYearMouthDay;
}

JS获取系统的指定定年月日的相关教程结束。

《JS获取系统的指定定年月日.doc》

下载本文的Word格式文档,以方便收藏与打印。