Dayjs日期处理类库

2022-07-17 09:50:50
2024-03-28 03:20:39

官方文档

https://dayjs.fenxianglu.cn/category/#node-js

安装命令

npm i dayjs -S

全局导入

import dayjs from "dayjs";
Vue.prototype.dayjs = dayjs;

所有可用解析标记的列表

标识 示例 描述
YY 18 年,两位数
YYYY 2018 年,四位数
M 1-12 月,从1开始
MM 01-12 月,两位数字
MMM Jan-Dec 月,英文缩写
D 1-31
DD 01-31 日,两位数
H 0-23 24小时
HH 00-23 24小时,两位数
h 1-12 12小时
hh 01-12 12小时,两位数
m 0-59 分钟
mm 00-59 分钟,两位数
s 0-59
ss 00-59 秒,两位数
S 0-9 毫秒(百),一位数
SS 00-99 毫秒(十),两位数
SSS 000-999 毫秒,三位数
Z -05:00 UTC偏移
ZZ -0500 UTC偏移,两位数
A AM / PM 上/下午,大写
a am / pm 上/下午,小写
Do 1st... 31st 月份的日期与序号

常用

时间戳-毫秒

dayjs(1639638979*1000).format("YYYY-MM-DD HH:mm:ss")    //   2021-12-16 15:16:19

时间戳-秒

dayjs.unix(1639638979).format("MM/DD/YYYY HH:mm:ss")    //   12/16/2021 15:16:19

时间文字格式 []表示转义,将字符放在方括号中,即可原样返回而不被格式化替换 (例如, [MM])。

dayjs.unix(1639638979).format("YYYY[年]MM[月]DD[日] HH[时]mm[分]ss[秒]")   //   2021年12月16日 15时16分19秒

基于公司项目二次封装

/*
 * @Author: 小白龙
 * @Date: 2022-01-26 16:28:58
 * @LastEditors: 小白龙
 * @LastEditTime: 2022-01-27 13:18:54
 * @Description: 基于公司常用项目时间格式化二次封装,默认时间戳为秒
 * @param {Number} time  时间戳
 * @param {String} format  时间格式
 */

import dayjs from "dayjs";
export default function formateDate(
  time = Date.parse(new Date()) / 1000,
  format
) {
  const dayPrefix = dayjs.unix(time);
  // 判断是否自定义格式
  if (typeof format === "string") return dayPrefix.format(format);

  const appObject = {
    hsing: "MM月DD日 HH:mm",
    singnow: "HH:mm DD[/]MM",
    gosing: "HH:mm MM[/]DD",
    singstar: "HH:mm DD[/]MM",
  };

  return dayPrefix.format(appObject[window._app || "singnow"]);
}
目录
暂无评论,欢迎留下你的评论

运营需要亿点资金维持,您的支持,是小白龙创作的动力!!!

昵称
留言
赞赏金额