js日期格式化7天前起止日期以及YYYY-MM-DD 原生实现


// YYYY-MM-DD 原生实现

console.log(Intl.DateTimeFormat("zh", { separator: "-", dateStyle: "short", timeStyle: "medium", }).format(),'DateTimeFormat',)
// 2023/12/16 09:14:55
console.log(new Date().toLocaleString("zh-CN", { separator: "-", /* year: "numeric", month: "2-digit", day: "2-digit"  */}),'toLocaleDateString',)
// 2023/12/16 09:14:55
console.log(Intl.DateTimeFormat("zh", { separator: "-", dateStyle: "short", timeStyle: "medium", }).format()?.replace(/\s|\W/g, '.'),'DateTimeFormat',)
// 2023.12.16.09.14.55
console.log(new Date().toLocaleString("zh-CN", { separator: "-", /* year: "numeric", month: "2-digit", day: "2-digit"  */})?.replace(/\s|\W/g, '.'),'toLocaleDateString',)
// 2023.12.16.09.14.55


//日期格式化函数 YYYY-MM-DD
formatDate(date) {
    var d = date ? new Date(date) : new Date();
    var month = '' + (d.getMonth() + 1);
    var day = '' + d.getDate();
    var year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
},




//七天前日期
const curDate = new Date().toLocaleDateString()
const endTime = curDate.replace(/[/]/g, '-')
const cur = new Date(); // 获取当前时间对象
const pastDate = new Date(cur.getTime() - (7 * 24 * 3600 * 1000))
const startTime = pastDate.toLocaleDateString().replace(/[/]/g, '-')

this.quickForm.docTime[0] = startTime
this.quickForm.docTime[1] = endTime


日期转json
console.log(today.toString());  //Mon Nov 11 2019 11:19:12 GMT+0800 (中国标准时间)
console.log(today.toISOString());  //2019-11-11T03:19:12.534Z
console.log(today.toTimeString());  //11:19:12 GMT+0800 (中国标准时间)
console.log(today.toUTCString());  //Mon, 11 Nov 2019 03:19:12 GMT
console.log(today.toLocaleDateString());  //2019/11/11
console.log(today.toLocaleString());        //2019/11/11 上午11:19:12
console.log(today.toLocaleString('chinese',{hour12:false}));        //2019/12/11 14:17:18
console.log(today.toLocaleTimeString());  //上午11:19:12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值