js獲取最近一周一個月三個月時間的簡單示例
獲取近一周時間
var?end?=?new?Date();
var?year?=?end.getFullYear();
var?month?=?end.getMonth()?+?1;//0-11表示1-12月
var?day?=?end.getDate();
var?dateObj?=?{};
dateObj.end?=?year?+?'-'?+?month?+?'-'?+?day;
if?(day?-?7?<=?0)?{???//如果在當月7日之前
????var?startMonthDay?=?new?Date(year,?(parseInt(month)?-?1),?0).getDate();????//1周前所在月的總天數(shù)
????if?(month?-?1?<=?0)?{?//如果在當年的1月份
??????dateObj.start?=?(year?-?1)?+?'-'?+?12?+?'-'?+?(31?-?(7?-?day));
????}?else?{
??????dateObj.start?=?year?+?'-'?+?(month?-?1)?+?'-'?+?(startMonthDay?-?(7?-?day));
????}
}?else?{
????dateObj.start?=?year?+?'-'?+?month?+?'-'?+?(day?-?7);
}
console.log(JSON.stringify(dateObj))
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.
獲取近一個月時間
var?end?=?new?Date();
var?year?=?end.getFullYear();
var?month?=?end.getMonth()?+?1;//0-11表示1-12月
var?day?=?end.getDate();
var?dateObj?=?{};
dateObj.end?=?year?+?'-'?+?month?+?'-'?+?day;
var?endMonthDay?=?new?Date(year,?month,?0).getDate();????//當前月的總天數(shù)
if(month?-?1?<=?0){?//如果是1月,年數(shù)往前推一年<br>
????dateObj.start?=?(year?-?1)?+?'-'?+?12?+?'-'?+?day;
}else{
????var?startMonthDay?=?new?Date(year,?(parseInt(month)?-?1),?0).getDate();
????if(startMonthDay?<?day){????//1個月前所在月的總天數(shù)小于現(xiàn)在的天日期
????????if(day?<?endMonthDay){????????//當前天日期小于當前月總天數(shù)
????????????dateObj.start?=?year?+?'-'?+?(month?-?1)?+?'-'?+?(startMonthDay?-?(endMonthDay?-?day));
????????}else{
????????????dateObj.start?=?year?+?'-'?+?(month?-?1)?+?'-'?+?startMonthDay;
????????}
????}else{
????????dateObj.start?=?year?+?'-'?+?(month?-?1)?+?'-'?+?day;
????}
}
console.log(JSON.stringify(dateObj))
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.
獲取近三個月時間
var?end?=?new?Date();
var?year?=?end.getFullYear();
var?month?=?end.getMonth()?+?1;//0-11表示1-12月
var?day?=?end.getDate();
var?dateObj?=?{};
dateObj.end?=?year?+?'-'?+?month?+?'-'?+?day;
var?endMonthDay?=?new?Date(year,?month,?0).getDate();????//當前月的總天數(shù)
if(month?-?3?<=?0){?//如果是1、2、3月,年數(shù)往前推一年
????var?start3MonthDay?=?new?Date((year?-?1),?(12?-?(3?-?parseInt(month))),?0).getDate();????//3個月前所在月的總天數(shù)
????if(start3MonthDay?<?day){????//3個月前所在月的總天數(shù)小于現(xiàn)在的天日期
????????dateObj.start?=?(year?-?1)?+?'-'?+?(12?-?(3?-?month))?+?'-'?+?start3MonthDay;
????}else{
????????dateObj.start?=?(year?-?1)?+?'-'?+?(12?-?(3?-?month))?+?'-'?+?day;
????}
}else{
????var?start3MonthDay?=?new?Date(year,?(parseInt(month)?-?3),?0).getDate();????//3個月前所在月的總天數(shù)
????if(start3MonthDay?<?day){????//3個月前所在月的總天數(shù)小于現(xiàn)在的天日期
????????if(day?<?endMonthDay){????????//當前天日期小于當前月總天數(shù),2月份比較特殊的月份
????????????dateObj.start?=?year?+?'-'?+?(month?-?3)?+?'-'?+?(start3MonthDay?-?(endMonthDay?-?day));
????????}else{
????????????dateObj.start?=?year?+?'-'?+?(month?-?3)?+?'-'?+?start3MonthDay;
????????}
????}else{
????????dateObj.start?=?year?+?'-'?+?(month?-?3)?+?'-'?+?day;
????}
}
console.log(JSON.stringify(dateObj))
New Date()與setDate()參數(shù)
相信網(wǎng)上已經(jīng)有很多關(guān)于日期的文章了,這里只是我自己再工作中遇到的問題然后加以總結(jié);
new Date()
new Date() 一共有六種形式,五種帶參數(shù)的一種不帶參數(shù)的;
- new Date();自然不用多說,默認獲取的是當前日期。
- new Date("month1 dd,yyyy hh:mm:ss"); 注意:參數(shù)是字符形式
- new Date("month1 dd,yyyy"); 注意:參數(shù)是字符形式
- new Date(yyyy,month2,dd,hh,mm,ss); 注意:參數(shù)不是字符
- new Date(yyyy,month2,dd); 注意:參數(shù)不是字符
- new Date(ms);?
參數(shù)說明:
month1:用英文,表示月份名稱;從January到December ;
dd:表示日期,1-31
yyyy:表示四位表示的年份
hh:mm:ss:表示時間,時(0-23)-分(0-59)-秒(0-59)
month2:是Number型的月份;從0-11;即1月到12月
ms:從1970年1月1日之間相差的毫秒數(shù)
特別提醒:有些是字符形式有些不是
總結(jié)
到此這篇關(guān)于js獲取最近一周一個月三個月時間的文章就介紹到這了,更多相關(guān)js獲取一周一個月三個月時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于IE中g(shù)etElementsByClassName不能用的問題解決方法
今天使用getElementsByClassName寫了段小程序,在IE6、8中測試的時候就出現(xiàn)問題了,瀏覽器報錯,經(jīng)搜索找到了一個比較好的方法,是個老外寫的一個方法2013-08-08
關(guān)于ckeditor在bootstrap中modal中彈框無法輸入的解決方法
今天小編就為大家分享一篇關(guān)于ckeditor在bootstrap中modal中彈框無法輸入的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

