js中日期的使用方法

  1. 判斷當前日期是不是當前年月的最后一天
    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. 獲取月份的最后一天
// 系統時間格式
console.log(new Date('2018','05',0)) //  Thu May 31 2018 00:00:00 GMT+0800 (中國標準時間)

// 取出數字
console.log(new Date('2021','02',0).getDate()) // 28
console.log(new Date('2020','02',0).getDate()) // 29
  1. 數組中日期排序
    來源于:https://www.cnblogs.com/matthew9298-Begin20160224/p/5433977.html
// 原數組
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,"/"));});
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容