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
欢迎转载,请注明出处
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步