New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

format-instant

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

format-instant

格式化时间

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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 PMAM PM
Q季度1到4

例子

  • 格式化年月日: 'xxxx年xx月xx日'
    formatInstant().format('YYYY年MM月DD日')
  • 格式化年月日: 'xxxx-xx-xx'
    formatInstant().format('YYYY-MM-DD')
  • 格式化时分秒(24小时制): 'xx时xx分xx秒'
    formatInstant().format('HH时mm分ss秒')
  • 格式化时分秒(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();//获取秒(时间戳)

Keywords

时间

FAQs

Package last updated on 20 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts