JavaScript日期操作(周,月,季度,年)

本文详细介绍了JavaScript中用于获取当前日期、本周日期、本月日期、本季日期、本年日期的相关函数,并展示了如何在实际场景中灵活运用这些函数进行日期计算与格式化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<mce:script type="text/javascript">

<!--

//当天

function showToDay()

{

var Nowdate=new Date();

M=Number(Nowdate.getMonth())+1

return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate();

}





//本周第一天

function showWeekFirstDay()



{

var Nowdate=new Date();

var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);

return WeekFirstDay;

}

//本周最后一天

function showWeekLastDay()

{

var Nowdate=new Date();

var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);

var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);

return WeekLastDay;

}

//本月第一天

function showMonthFirstDay()

{

var Nowdate=new Date();

var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1);

return MonthFirstDay;

}



//本月最后一天



function showMonthLastDay()

{

var Nowdate=new Date();

var tmpDate=new Date(Nowdate.getYear(),Nowdate.getMonth()+1,1);

//tmpDate.setDate(tmpDate.getDate() -1);

//return new Date(Nowdate.getYear(),Nowdate.getMonth(),tmpDate.getDate());

var MonthLastDay=new Date(tmpDate-86400000);

return MonthLastDay;



}





//本季第一天

function showquarterFirstDay()

{

var Nowdate=new Date();

if(Nowdate.getMonth()<3)

return new Date(Nowdate.getYear(),0,1);

else if(Nowdate.getMonth()>2 && Nowdate.getMonth()<6)

return new Date(Nowdate.getYear(),3,1);

else if(Nowdate.getMonth()>5 && Nowdate.getMonth()<9)

return new Date(Nowdate.getYear(),6,1);

else if(Nowdate.getMonth()>8)

return new Date(Nowdate.getYear(),9,1);

}



//本季最后一天

function showquarterLastDay()

{

var Nowdate=new Date();



if(Nowdate.getMonth()<3)

return new Date(Nowdate.getYear(),2,31);

else if(Nowdate.getMonth()>2 && Nowdate.getMonth()<6)

return new Date(Nowdate.getYear(),5,30);

else if(Nowdate.getMonth()>5 && Nowdate.getMonth()<9)

return new Date(Nowdate.getYear(),8,30);

else if(Nowdate.getMonth()>8)

return new Date(Nowdate.getYear(),11,31);

}





//本年第一天

function showyearFirstDay()

{

var Nowdate=new Date();

var yearFirstDay=new Date(Nowdate.getYear(),0,1);

return yearFirstDay;

}



//本年最后一天

function showyearLastDay()

{

var Nowdate=new Date();

var yearLastDay=new Date(Nowdate.getYear(),11,31);

return yearLastDay;

}





//当前月

function showmonthCurrent()

{

var Nowdate=new Date();

var Month=Nowdate.getMonth()+1;

Month=(Month<10)?'-0'+Month:'-'+Month;

return Nowdate.getYear()+Month;

}



//本季开始年月

function showquarterFirstMonth()

{

var Nowdate=new Date();

if(Nowdate.getMonth()<3)

return Nowdate.getYear()+'-01';

else if(Nowdate.getMonth()>2 && Nowdate.getMonth()<6)

return Nowdate.getYear()+'-04';

else if(Nowdate.getMonth()>5 && Nowdate.getMonth()<9)

return Nowdate.getYear()+'-07';

else if(Nowdate.getMonth()>8)

return Nowdate.getYear()+'-10';

}



//本季最后年月

function showquarterLastMonth()

{

var Nowdate=new Date();

if(Nowdate.getMonth()<3)

return Nowdate.getYear()+'-03';

else if(Nowdate.getMonth()>2 && Nowdate.getMonth()<6)

return Nowdate.getYear()+'-06';

else if(Nowdate.getMonth()>5 && Nowdate.getMonth()<9)

return Nowdate.getYear()+'-09';

else if(Nowdate.getMonth()>8)

return Nowdate.getYear()+'-12';

}





//本年开始年月

function showyearFirstMonth()

{

var Nowdate=new Date();

return Nowdate.getYear()+'-01';

}



//本年最后年月

function showyearLastMonth()

{

var Nowdate=new Date();

return Nowdate.getYear()+'-12';

}

//格式化日期格式

function Date.prototype.toString(){

return this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate();

}

// -->

</mce:script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iguohao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值