js 獲取當前時間的前一天,后一天


new Date(new Date().getTime() - 24*60*60*1000); //前一天
new Date(new Date().getTime() + 24*60*60*1000); //后一天

 

var myDate=new Date('22021/12/30');

console.log(myDate) //Thu Dec 30 22021 00:00:00 GMT+0800 (中國標准時間)

myDate.setDate(myDate.getDate()-1);
console.log(myDate) //Wed Dec 29 22021 00:00:00 GMT+0800 (中國標准時間)

 

 

 vue寫法:

 1 /**
 2  * 日期格式化
 3  */
 4 export function dateFormat (date, format) {
 5   format = format || 'yyyy-MM-dd hh:mm:ss'
 6   if (date !== 'Invalid Date') {
 7     const o = {
 8       'M+': date.getMonth() + 1, // month
 9       'd+': date.getDate(), // day
10       'h+': date.getHours(), // hour
11       'm+': date.getMinutes(), // minute
12       's+': date.getSeconds(), // second
13       'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
14       S: date.getMilliseconds() // millisecond
15     }
16     if (/(y+)/.test(format)) {
17       format = format.replace(RegExp.$1,
18         (date.getFullYear() + '').substr(4 - RegExp.$1.length))
19     }
20     for (const k in o) {
21       if (new RegExp('(' + k + ')').test(format)) {
22         format = format.replace(RegExp.$1,
23           RegExp.$1.length === 1 ? o[k]
24             : ('00' + o[k]).substr(('' + o[k]).length))
25       }
26     }
27     return format
28   }
29   return ''
30 }

 調用方式:

 import { dateFormat } from '@/utils/date'

const startT = dateFormat(item.value[0],'yyyy-MM-dd')
 
 
dateFormat(new Date(new Date().getFullYear() ,0),'yyyy-MM-dd')//當前年份的第一天
dateFormat(new Date(new Date().getFullYear() + 1, 0, 0),'yyyy-MM-dd')//當前年份的最后一天
 
如圖:

 

時間戳的轉換調用:

dateFormat(new Date('1291189127000'))  ==> 2010-12-01 15:38:47

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM