当前位置: 首页 > news >正文

【vue】设置时间格式

文章目录

  • 时间格式处理的方式
    • 方式一:Moment.js
      • 引入
      • 设定区域
      • 使用
        • 获取时间
        • 设置时间
        • (15)年份-1
        • 格式化时间
    • 方式二:dayjs
      • 引入
      • 使用
        • 创建日期对象
        • 格式化日期
        • 解析日期(字符串转日期)

时间格式处理的方式

方式一:Moment.js

引入

1.安装
npm install moment 或者 yarn add moment
2.引用

// require 方式
var moment = require('moment');// import 方式
import moment from 'moment'; //浏览器方式引入
<script src="moment.js"></script>

设定区域

设定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 引入

<script src="https://unpkg.com/dayjs@1.11.10/dayjs.min.js"></script>

使用

创建日期对象
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() 方法自定义日期输出:

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 插件:

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

参考博客
参考博客

http://www.dtcms.com/a/491015.html

相关文章:

  • 泰安网站建设方案为网站做一则广告
  • 23种设计模式——状态模式(State Pattern)
  • 管理系统布局框架中都包含哪些要素?导航、面包屑、tab等
  • 制作企业网站的app如何做网校网站
  • 卷积值提取
  • [Android] Alarm Clock Pro 11.1.0一款经典简约个性的时钟
  • 【机器学习算法篇】K-近邻算法
  • K8S高可用集群-二进制部署 + nginx-proxy静态Pod版本
  • 使用Open CASCADE和BRepOffsetAPI_MakePipeShell创建螺旋槽钻头三维模型
  • 邯郸网站制作多少钱网站如何收录
  • 如何区分Android、Android Automotive、Android Auto
  • 企业融资方式有哪几种淄博网站seo价格
  • python - 第五天
  • 凡科网的网站建设怎么做网站 建设 公司
  • 透过浏览器原理学习前端三剑客:HTML、CSS与JavaScript
  • 镇江市网站建设江西省建设厅教育网站上查询
  • dede网站怎么设置首页相亲网站透露自己做理财的女生
  • Docker在已经构建好的镜像中安装包
  • 智慧物流赛项竞赛内容与技能要求深度解析
  • GPU散热革命:NVIDIA微通道液冷板(MLCP)技术深度解析
  • Docker安装部署MySQL一主二从集群
  • 搭建网站服务器多少钱网站在建设中是什么意思
  • Java 11对集合类做了哪些增强?
  • SQLSugar框架数据库优先
  • 工程建设教育网站北京网站建设cnevo
  • Vector数据库性能大比武:Pinecone、Weaviate、Chroma速度与准确率实测
  • 天津老区建设促进会网站移动开发的现状和前景
  • 笔试强训(六)
  • Iterator迭代器 【ES6】
  • spring boot实现接口数据脱敏,整合jackson实现敏感信息隐藏脱敏