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

 

 

posted @ 2022-03-17 17:02  星期7  阅读(5425)  评论(0编辑  收藏  举报