【vue】设置时间格式

文章目录

时间格式处理的方式

方式一:Moment.js

引入

1.安装
npm install moment 或者 yarn add moment

2.引用

css 复制代码
// require 方式
var moment = require('moment');

// import 方式
import moment from 'moment'; 

//浏览器方式引入
<script src="moment.js"></script>

设定区域

css 复制代码
设定moment区域为中国
// require 方式
require('moment/locale/zh-cn')
moment.locale('zh-cn'); 

// import 方式
import 'moment/locale/zh-cn'
moment.locale('zh-cn');   

使用

获取时间

1)获取当前时间

moment()

(2)获取今天0时0分0秒

moment().startOf('day')

(3)获取本周第一天(周日)0时0分0秒

moment().startOf('week')

(4)获取本周周一0时0分0秒

moment().startOf('isoWeek')

(5)获取当前月第一天0时0分0秒

moment().startOf('month')

(6)获取今天23时59分59秒

moment().endOf('day')

(7)获取本周最后一天(周六)23时59分59秒

moment().endOf('week')

(8)获取本周周日23时59分59秒

moment().endOf('isoWeek')

(9)获取当前月最后一天23时59分59秒

moment().endOf('month')

(10)获取当前月的总天数

moment().daysInMonth()

(11)获取时间戳(以秒为单位)

moment().format('X') // 返回值为字符串类型

moment().unix() // 返回值为数值型

(12)获取时间戳(以毫秒为单位)

moment().format('x') // 返回值为字符串类型

moment().valueOf() // 返回值为数值型

(13)获取年份

moment().year()

moment().get('year')

(14)获取月份

moment().month() // (0~11, 0: January, 11: December)

moment().get('month')

(15)获取一个月中的某一天

moment().date()

moment().get('date')

(16)获取一个星期中的某一天

moment().day() // (0~6, 0: Sunday, 6: Saturday)

moment().weekday() // (0~6, 0: Sunday, 6: Saturday)

moment().isoWeekday() // (1~7, 1: Monday, 7: Sunday)

moment().get('day')

mment().get('weekday')

moment().get('isoWeekday')

(17)获取小时

moment().hours()

moment().get('hours')

(18)获取分钟

moment().minutes()

moment().get('minutes')

(19)获取秒数

moment().seconds()

moment().get('seconds')

(20)获取当前的年月日时分秒

moment().toArray() // years, months, date, hours, minutes, seconds, milliseconds

moment().toObject() // {years: xxxx, months: x, date: xx ...}

设置时间

(1)设置年份

moment().year(2019)

moment().set('year', 2019)

moment().set({year: 2019})

(2)设置月份

moment().month(11) // (0~11, 0: January, 11: December)

moment().set('month', 11)

(3)设置某个月中的某一天

moment().date(15)

moment().set('date', 15)

(4)设置某个星期中的某一天

moment().weekday(0) // 设置日期为本周第一天(周日)

moment().isoWeekday(1) // 设置日期为本周周一

moment().set('weekday', 0)

moment().set('isoWeekday', 1)

(5)设置小时

moment().hours(12)

moment().set('hours', 12)

(6)设置分钟

moment().minutes(30)

moment().set('minutes', 30)

(7)设置秒数

moment().seconds(30)

moment().set('seconds', 30)

(8)年份+1

moment().add(1, 'years')

moment().add({years: 1})

(9)月份+1

moment().add(1, 'months')

(10)日期+1

moment().add(1, 'days')

(11)星期+1

moment().add(1, 'weeks')

(12)小时+1

moment().add(1, 'hours')

(13)分钟+1

moment().add(1, 'minutes')

(14)秒数+1

moment().add(1, 'seconds')

(15)年份-1

moment().subtract(1, 'years')

moment().subtract({years: 1})

(16)月份-1

moment().subtract(1, 'months')

(17)日期-1

moment().subtract(1, 'days')

(18)星期-1

moment().subtract(1, 'weeks')

(19)小时-1

moment().subtract(1, 'hours')

(20)分钟-1

moment().subtract(1, 'minutes')

(21)秒数-1

moment().subtract(1, 'seconds')

格式化时间

(1)格式化年月日: 'xxxx年xx月xx日'

moment().format('YYYY年MM月DD日')

(2)格式化年月日: 'xxxx-xx-xx'

moment().format('YYYY-MM-DD')

(3)格式化时分秒(24小时制): 'xx时xx分xx秒'

moment().format('HH时mm分ss秒')

(4)格式化时分秒(12小时制):'xx:xx:xx am/pm'

moment().format('hh:mm:ss a')

(5)格式化时间戳(以毫秒为单位)

moment().format('x') // 返回值为字符串类型

方式二:dayjs

快速 2kB 的 Moment.js 替代品,拥有相同的现代 API

引入

npm install dayjsyarn add dayjs

CDN 引入

css 复制代码
<script src="https://unpkg.com/dayjs@1.11.10/dayjs.min.js"></script>

使用

创建日期对象
css 复制代码
import dayjs from 'dayjs';

// 当前时间
const now = dayjs();

// 指定日期字符串(支持多种格式)
const d1 = dayjs('2024-06-01');
const d2 = dayjs('2024/06/01 12:30:00');

// 指定时间戳(毫秒)
const d3 = dayjs(1717200000000);

// 通过原生 Date 对象
const d4 = dayjs(new Date());

// 通过数组(不推荐,Day.js 不直接支持)
格式化日期

使用 .format() 方法自定义日期输出:

css 复制代码
const date = dayjs('2024-06-01 15:30:45');
console.log(date.format('YYYY-MM-DD HH:mm:ss')); // 2024-06-01 15:30:45
console.log(date.format('dddd, MMMM D, YYYY'));  // Saturday, June 1, 2024
console.log(date.format('YYYY年M月D日'));         // 2024年6月1日
解析日期(字符串转日期)

Day.js 默认只支持 ISO 8601 格式和部分常见格式。如果需要解析自定义格式,需引入 customParseFormat 插件:

css 复制代码
import customParseFormat from 'dayjs/plugin/customParseFormat';
dayjs.extend(customParseFormat);

const d = dayjs('2024年06月01日', 'YYYY年MM月DD日');
console.log(d.format()); // 2024-06-01T00:00:00+08:00

参考博客
参考博客

相关推荐
枫叶梨花11 小时前
Electrobun透明窗口在Windows上的输入穿透排查
前端·electron·bun
卤蛋fg611 小时前
vue 甘特图组件 vxe-gantt 依赖连接线可视化拖拽创建与双击删除
vue.js
梦醒沉醉11 小时前
5、表达式与运算符
javascript
云上工程笔记12 小时前
AstraFlow + React/Vite:如何用自然语言开发并部署一个 AI 咖啡网站原型
前端·人工智能·react.js
Q一件事13 小时前
ArcGIS中TypeError: can‘t multiply sequence by non-int of type ‘str‘错误
前端·javascript·arcgis
美狐美颜SDK开放平台13 小时前
从视频处理到实时渲染:直播APP中视频美颜SDK的关键技术解析
前端·人工智能·架构·实时互动·音视频·视频美颜sdk
WeiXin_DZbishe13 小时前
基于springboot大学生提问箱系统-计算机毕设【课程设计】72593
javascript·vue.js·spring boot·vscode·python·node.js·php
yume_sibai13 小时前
ES6 核心知识点完全总结(变量 + 箭头函数 + 解构 + Promise + 模块化)
前端·ecmascript·es6
Moment13 小时前
为什么市面上的 coding agent 大多数都基于Nodejs?
前端·后端·面试
吃瓜的猹14 小时前
Gemini 3.8 Flash 是真的站起来了,一起来看看我到底做了什么吧~
前端