日期时间格式转换

学习目标:

  • 掌握日期格式转换

学习产出:

后端
在这里插入图片描述

前端:日期时间渲染
法一:使用Moment.js
Moment.js是一款流行的 JavaScript 日期库,提供了丰富的日期和时间处理函数,可以方便地对日期和时间进行操作。

1、npm install moment
2、可以使用moment()方法来创建一个Moment对象。例如:

const now = moment();
console.log(now); // moment("2023-06-11T09:00:00.000")

3、可以使用format()方法来格式化日期和时间。例如:

const now = moment();
const formatted = now.format('YYYY-MM-DD HH:mm:ss');
console.log(formatted); // 2023-06-11 09:00:00

4、可以使用add()方法和subtract()方法来增加或减少日期和时间。例如:

const now = moment();
const nextWeek = now.add(1, 'weeks');
const lastWeek = now.subtract(1, 'weeks');
console.log(nextWeek.format('YYYY-MM-DD')); // 2023-06-18
console.log(lastWeek.format('YYYY-MM-DD')); // 2023-06-04

5、可以使用startOf()方法和endOf()方法来获取日期和时间的开始和结束。例如:

const now = moment();
const startOfToday = now.startOf('day');
const endOfToday = now.endOf('day');
console.log(startOfToday.format('YYYY-MM-DD HH:mm:ss')); // 2023-06-11 00:00:00
console.log(endOfToday.format('YYYY-MM-DD HH:mm:ss')); // 2023-06-11 23:59:59

6、可以使用diff()方法来计算两个日期之间的差值。例如:

const start = moment('2023-06-01');
const end = moment('2023-06-11');
const diff = end.diff(start, 'days');
console.log(diff); // 10

首先 npm install moment
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

法二:使用原生JavaScript–在函数中直接转换

错误解决 “date.getFullYear is not a function“
一般出现这种情况,表明日期对象不是有效的 Date 类型,或者未正确初始化。
解决办法是使用 new Date() 构造函数将其转换为 Date 对象。
在这里插入图片描述

获取日期和时间的各个部分:
可以使用getFullYear()、getMonth()、getDate()、getDay()、getHours()、getMinutes()、getSeconds()getMilliseconds()方法来获取日期和时间的各个部分。其中,getMonth()返回的是月份 (0-11)getDay()返回的是星期几 (0-6)。例如:

const now = new Date();
console.log(now.getFullYear()); // 2023
console.log(now.getMonth()); // 5
console.log(now.getDate()); // 11
console.log(now.getDay()); // 5
console.log(now.getHours()); // 9
console.log(now.getMinutes()); // 0
console.log(now.getSeconds()); // 0
console.log(now.getMilliseconds()); // 0

MM
表示月份,例如:06。

const now = new Date();
const month = now.getMonth() + 1;
console.log(month);//6
console.log(month.toString().padStart(2, '0')); // 06

格式化日期和时间
可以使用toLocaleDateString()、toLocaleTimeString()toLocaleString()方法来格式化日期和时间。其中,toLocaleDateString()返回的是日期字符串,toLocaleTimeString()返回的是时间字符串,toLocaleString()返回的是日期和时间字符串。例如:

const now = new Date();
console.log(now.toLocaleDateString()); // 2023/6/11
console.log(now.toLocaleTimeString()); // 09:00:00
console.log(now.toLocaleString()); // 2023/6/11 9:00:00

时间戳: 时间戳是指从1970年1月1日00:00:00 UTC开始计算的毫秒数。可以使用Date.now()方法来获取当前时间戳。例如:

const timestamp = Date.now();
console.log(timestamp); // 1654933200000

法三:使用Day.js
可以使用dayjs()方法来创建一个Day.js对象。例如:

const now = dayjs();
console.log(now); // dayjs("2023-06-11T09:00:00.000Z")

可以使用format()方法来格式化日期和时间。例如:

const now = dayjs();
const formatted = now.format('YYYY-MM-DD HH:mm:ss');
console.log(formatted); // 2023-06-11 09:00:00

可以使用add()方法和subtract()方法来增

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值