js中日期的使用方法

  1. 判斷當(dāng)前日期是不是當(dāng)前年月的最后一天
    function isLastDay(inputDate) {
      var d = new Date(inputDate.replace(/\-/, "/ "));
      var nd = new Date(d.getTime() + 24 * 60 * 60 * 1000); //next day
      return d.getMonth() != nd.getMonth();
    }
cosole.log("isLastDay",isLastDay("2000-02-29")); // true
cosole.log("isLastDay",isLastDay("2001-02-29")); // false
  1. 獲取月份的最后一天
// 系統(tǒng)時(shí)間格式
console.log(new Date('2018','05',0)) //  Thu May 31 2018 00:00:00 GMT+0800 (中國標(biāo)準(zhǔn)時(shí)間)

// 取出數(shù)字
console.log(new Date('2021','02',0).getDate()) // 28
console.log(new Date('2020','02',0).getDate()) // 29
  1. 數(shù)組中日期排序
    來源于:https://www.cnblogs.com/matthew9298-Begin20160224/p/5433977.html
// 原數(shù)組
var timeArr=[       
{'id':'A01','date':'2016-04-20 23:22:11'},      
{'id':'A02','date':'2016-04-21 21:00:11'},      
{'id':'A03','date':'2016-04-23 22:00:22'},      
{'id':'A04','date':'2016-04-19 12:22:00'},      
{'id':'A05','date':'2016-02-19 11:11:00'}   
];
timeArr.sort(function(a,b) {return Date.parse(b.date.replace(/-/g,"/"))-Date.parse(a.date.replace(/-/g,"/"));});
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容