format-instant

1.0.0 • Public • Published

format-instant

介绍

一款时间格式化的插件,目前只在公司内部测试使用。

安装教程

npm install format-instant -S

使用说明

    import formatInstant from "format-instant"
    formatInstant().format("YYYY-MM-DD HH:mm:ss");  //2023-09-25 10:25:30
	formatInstant("2023-09-25").add(5,"days").format("YYYY-MM-DD HH:mm:ss"); //2023-09-30 15:30:25

格式化时间

格式代码 说明 返回值
YY 两位数字表示的年份 如:23 或 15
YYYY 四位数字完整表示的年份 如:2023 或 2015
M 数字表示的月份,没有前导零 1到12
MM 数字表示的月份,有前导零 01到12
MMM 三个字母缩写表示的月份 Jan到Dec
MMMM 月份,完整的文本格式 January到December
D 月份中的第几天,没有前导零 1到31
DD 月份中的第几天,有前导零 01到31
DI 月份中的第几天 一月到十二月
DN 月份中的第几天 1月到12月
d 星期中的第几天,数字表示 0到6,0表示周日,6表示周六
dd 星期中的第几天 星期日、星期一到星期六
ddd 三个字母表示星期中的第几天 Sun到Sat
dddd 星期几,完整的星期文本 从Sunday到Saturday
dt 星期中的第几天 周日、周一到周六
di 星期中的第几天 日、一到六
HH 小时,24小时制,有前导零 00到23
H 小时,24小时制,无前导零 0到23
hh 小时,12小时制,有前导零 00到12
h 小时,12小时制,无前导零 0到12
mm 有前导零的分钟数 00到59
m 没有前导零的分钟数 0到59
ss 有前导零的秒数 01到59
s 没有前导零的秒数 1到59
A 大写的AM PM AM PM
Q 季度 1到4

例子

  1. 格式化年月日: 'xxxx年xx月xx日'
    formatInstant().format('YYYY年MM月DD日')
  1. 格式化年月日: 'xxxx-xx-xx'
    formatInstant().format('YYYY-MM-DD')
  1. 格式化时分秒(24小时制): 'xx时xx分xx秒'
    formatInstant().format('HH时mm分ss秒')
  1. 格式化时分秒(12小时制):'xx:xx:xx am/pm'
    formatInstant().format('hh:mm:ss a')

相对时间

    formatInstant("2011-10-31").fromNow(); // 8 年前
    formatInstant("20120620").fromNow(); // 7 年前
    formatInstant().startOf('date').fromNow();        // 20 小时前
    formatInstant().endOf('date').fromNow();          // 4 小时内

formatInstant 参数 'YYYY-MM-DD'、'YYYY-MM' 'YYYY/MM/DD' 'YYYY/MM' 'YYYY.MM.DD' 'YYYY.MM' 'YYYYMMDD' 'YYYYMM' 'HH:mm:ss.SSSS' 'HH:mm:ss,SSSS' 'HH:mm:ss' 'HH:mm' formatInstant() toArray或toObject返回的数组和对象


startOf 获取当前年、月、日等第一天0时0分0秒 参数 "year" "month" "date" "week" "isoWeek"


endOf 获取当前年、月、日等最后一天23时59分59秒 参数 "year" "month" "date" "week" "isoWeek"

日历时间

    formatInstant().subtract(10, 'dates').calendar(); // 2019年5月14日
    formatInstant().subtract(6, 'dates').calendar();  // 上周六晚上7点49
    formatInstant().subtract(3, 'dates').calendar();  // 本周二晚上7点49
    formatInstant().subtract(1, 'dates').calendar();  // 昨天晚上7点49分
    formatInstant().calendar();                      // 今天晚上7点49分
    formatInstant().add(1, 'dates').calendar();       // 明天晚上7点49分
    formatInstant().add(3, 'dates').calendar();       // 下周一晚上7点49
    formatInstant().add(10, 'dates').calendar();      // 2019年6月3日
	formatInstant().get("years"); //2023

add 参数 "years" "months" "dates" "hours" "minutes" "seconds"


get 参数 "years" "months" "dates" "hours" "minutes" "seconds" "weeks" "isoWeek"


subtract 是减去的意思 参数 "years" "months" "dates" "hours" "minutes" "seconds"

快速示例

   formatInstant().formatFast('L');    // 2019-05-24
   formatInstant().formatFast('l');    // 2019-05-24
   formatInstant().formatFast('LL');   // 2019年5月24日
   formatInstant().formatFast('ll');   // 2019年5月24日
   formatInstant().formatFast('LLL');  // 2019年5月24日晚上7点50分
   formatInstant().formatFast('lll');  // 2019年5月24日晚上7点50分
   formatInstant().formatFast('LLLL'); // 2019年5月24日星期五晚上7点50分
   formatInstant().formatFast('llll'); // 2019年5月24日星期五晚上7点50分

比较两个时间

   var c = formatInstant("2008-09");
   var d = formatInstant("2007-00");
   console.log(c.diff(d, 'years')); // 1
   console.log(c.diff(d, 'years', true)) // 1.75
   var e = formatInstant();
   var f = formatInstant().add(1, 'seconds');
   console.log(e.diff(f)) // -1000 (毫秒)
   console.log(f.diff(e)) // 1000 (毫秒)

diff 参数 除了"years" 还有 "months" "dates" "hours" "minutes" "seconds" "weeks"

其他

   formatInstant().year(); // 当前年
   formatInstant().month(); // 当前月
   formatInstant().date(); // 当前日
   formatInstant().hour(); // 当前小时
   formatInstant().minute(); // 当前分钟
   formatInstant().second(); // 当前秒
   formatInstant().week(); // 0-6 0周日 6周末 开始日是周日
   formatInstant().isoWeek(); // 1-6 开始日是周一
   formatInstant().toArray();//[2023,10,25,10,26,30]年月日时分秒
   formatInstant().toObject();//{years:2023,months:10,dates:25,hours:10,minutes:26,seconds:30}年月日时分秒
   formatInstant().daysInMonth();//获取当前月的总天数
   formatInstant().valueof();//获取毫秒(时间戳)
   formatInstant().unix();//获取秒(时间戳)

Package Sidebar

Install

npm i format-instant

Weekly Downloads

0

Version

1.0.0

License

ISC

Unpacked Size

68.7 kB

Total Files

24

Last publish

Collaborators

  • liqc