欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js獲取最近一周一個月三個月時間的簡單示例

 更新時間:2021年12月28日 11:45:48   作者:云_  
時間的獲取和格式化是我們經(jīng)常遇到的問題,下面這篇文章主要給大家介紹了關(guān)于利用js如何獲取最近一周一個月三個月時間的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下

獲取近一周時間

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)?{???//如果在當(dāng)月7日之前
????var?startMonthDay?=?new?Date(year,?(parseInt(month)?-?1),?0).getDate();????//1周前所在月的總天數(shù)
????if?(month?-?1?<=?0)?{?//如果在當(dāng)年的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();????//當(dāng)前月的總天數(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){????????//當(dāng)前天日期小于當(dāng)前月總天數(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();????//當(dāng)前月的總天數(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){????????//當(dāng)前天日期小于當(dāng)前月總天數(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ù)的;

  1. new Date();自然不用多說,默認(rèn)獲取的是當(dāng)前日期。
  2. new Date("month1 dd,yyyy hh:mm:ss"); 注意:參數(shù)是字符形式
  3. new Date("month1 dd,yyyy"); 注意:參數(shù)是字符形式
  4. new Date(yyyy,month2,dd,hh,mm,ss); 注意:參數(shù)不是字符
  5. new Date(yyyy,month2,dd); 注意:參數(shù)不是字符
  6. 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)文章

最新評論