Socket
Socket
Sign inDemoInstall

dayjs

Package Overview
Dependencies
Maintainers
1
Versions
126
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dayjs - npm Package Compare versions

Comparing version 1.5.16 to 1.5.17

.eslintrc.json

2

dist/dayjs.min.js

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.dayjs=t()}(this,function(){"use strict";function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}var t="second",n="minute",r="hour",s="day",i="week",a="month",u="year",c=function(e,t,n){return!e||e.length>=t?e:"".concat(Array(t+1-e.length).join(n)).concat(e)},o=function(e){return e&&String(e).toLowerCase().replace(/s$/,"")},h=function(e){var t;return null===e?new Date(NaN):e?e instanceof Date?e:(t=String(e).match(/^(\d{4})-?(\d{2})-?(\d{1,2})$/))?new Date(t[1],t[2]-1,t[3]):new Date(e):new Date},l=function(){function l(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,l),this.$d=h(e),this.init()}var d,$,f;return d=l,($=[{key:"init",value:function(){this.$zone=this.$d.getTimezoneOffset()/60,this.$zoneStr=c(String(-1*this.$zone).replace(/^(.)?(\d)/,"$10$200"),5,"+"),this.$y=this.$d.getFullYear(),this.$M=this.$d.getMonth(),this.$D=this.$d.getDate(),this.$W=this.$d.getDay(),this.$H=this.$d.getHours(),this.$m=this.$d.getMinutes(),this.$s=this.$d.getSeconds(),this.$ms=this.$d.getMilliseconds()}},{key:"isValid",value:function(){return!("Invalid Date"===this.$d.toString())}},{key:"isLeapYear",value:function(){return this.$y%4==0&&this.$y%100!=0||this.$y%400==0}},{key:"isSame",value:function(e){return this.valueOf()===e.valueOf()}},{key:"isBefore",value:function(e){return this.valueOf()<e.valueOf()}},{key:"isAfter",value:function(e){return this.valueOf()>e.valueOf()}},{key:"year",value:function(){return this.$y}},{key:"month",value:function(){return this.$M}},{key:"date",value:function(){return this.$D}},{key:"hour",value:function(){return this.$H}},{key:"minute",value:function(){return this.$m}},{key:"second",value:function(){return this.$s}},{key:"millisecond",value:function(){return this.$ms}},{key:"unix",value:function(){return Math.floor(this.valueOf()/1e3)}},{key:"valueOf",value:function(){return this.$d.getTime()}},{key:"startOf",value:function(e){var c=this,h=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],d=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:c.$y,r=new l(new Date(n,t,e));return h?r:r.endOf(s)},$=function(e,t){return new l(c.toDate()[e].apply(c.toDate(),h?[0,0,0,0].slice(t):[23,59,59,999].slice(t)))};switch(o(e)){case u:return h?d(1,0):d(31,11,this.$y);case a:return h?d(1,this.$M):d(0,this.$M+1,this.$y);case i:return h?d(this.$D-this.$W,this.$M):d(this.$D+(6-this.$W),this.$M,this.$y);case s:case"date":return $("setHours",0);case r:return $("setMinutes",1);case n:return $("setSeconds",2);case t:return $("setMilliseconds",3);default:return this.clone()}}},{key:"endOf",value:function(e){return this.startOf(e,!1)}},{key:"mSet",value:function(e,s){switch(o(e)){case"date":this.$d.setDate(s);break;case a:this.$d.setMonth(s);break;case u:this.$d.setFullYear(s);break;case r:this.$d.setHours(s);break;case n:this.$d.setMinutes(s);break;case t:this.$d.setSeconds(s);break;case"millisecond":this.$d.setMilliseconds(s)}return this.init(),this}},{key:"set",value:function(e,t){return n=t,Number.isNaN(parseFloat(n))||!Number.isFinite(n)?this:this.clone().mSet(e,t);var n}},{key:"add",value:function(e,t){var c,h=t&&1===t.length?t:o(t);if(["M",a].indexOf(h)>-1){var d=this.set("date",1).set(a,this.$M+e);return d=d.set("date",Math.min(this.$D,d.daysInMonth()))}if(["y",u].indexOf(h)>-1)return this.set(u,this.$y+e);switch(h){case"m":case n:c=6e4;break;case"h":case r:c=36e5;break;case"d":case s:c=864e5;break;case"w":case i:c=6048e5;break;default:c=1e3}return new l(this.valueOf()+e*c)}},{key:"subtract",value:function(e,t){return this.add(-1*e,t)}},{key:"format",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DDTHH:mm:ssZ",n="Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday".split("."),r="January.February.March.April.May.June.July.August.September.October.November.December".split(".");return t.replace(/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g,function(t){switch(t){case"YY":return String(e.$y).slice(-2);case"YYYY":return String(e.$y);case"M":return String(e.$M+1);case"MM":return c(String(e.$M+1),2,"0");case"MMM":return r[e.$M].slice(0,3);case"MMMM":return r[e.$M];case"D":return String(e.$D);case"DD":return c(String(e.$D),2,"0");case"d":return String(e.$W);case"dddd":return n[e.$W];case"H":return String(e.$H);case"HH":return c(String(e.$H),2,"0");case"m":return String(e.$m);case"mm":return c(String(e.$m),2,"0");case"s":return String(e.$s);case"ss":return c(String(e.$s),2,"0");case"Z":return"".concat(e.$zoneStr.slice(0,-2),":00");default:return e.$zoneStr}})}},{key:"diff",value:function(e,n){var r,c,h,d,$,f,y=arguments.length>2&&void 0!==arguments[2]&&arguments[2],v=o(n),m=e instanceof l?e:new l(e),k=this-m,M=(r=this,d=12*((c=m).year()-r.year())+(c.month()-r.month()),$=r.clone().add(d,"months"),h=c-$<0?(c-$)/($-r.clone().add(d-1,"months")):(c-$)/(r.clone().add(d+1,"months")-$),Number(-(d+h))||0);switch(v){case u:M/=12;break;case a:break;case"quarter":M/=3;break;case i:M=k/6048e5;break;case s:M=k/864e5;break;case t:M=k/1e3;break;default:M=k}return y?M:(f=M)<0?Math.ceil(f)||0:Math.floor(f)}},{key:"daysInMonth",value:function(){return this.endOf(a).$D}},{key:"clone",value:function(){return new l(this)}},{key:"toDate",value:function(){return new Date(this.$d)}},{key:"toArray",value:function(){return[this.$y,this.$M,this.$D,this.$H,this.$m,this.$s,this.$ms]}},{key:"toJSON",value:function(){return this.toISOString()}},{key:"toISOString",value:function(){return this.toDate().toISOString()}},{key:"toObject",value:function(){return{years:this.$y,months:this.$M,date:this.$D,hours:this.$H,minutes:this.$m,seconds:this.$s,milliseconds:this.$ms}}},{key:"toString",value:function(){return this.$d.toUTCString()}}])&&e(d.prototype,$),f&&e(d,f),l}();return function(e){return new l(e)}});
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.dayjs=t()}(this,function(){"use strict";function e(e,t){for(var n=0;n<t.length;n++){var s=t[n];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),Object.defineProperty(e,s.key,s)}}var t="second",n="minute",s="hour",r="day",i="week",a="month",u="year",c="Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday".split("."),o="January.February.March.April.May.June.July.August.September.October.November.December".split("."),h=/^(\d{4})-?(\d{2})-?(\d{1,2})$/,l=/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}/g,$=function(e,t,n){var s=String(e);return!e||s.length>=t?e:"".concat(Array(t+1-s.length).join(n)).concat(e)},d=function(e){return e&&String(e).toLowerCase().replace(/s$/,"")},f=function(e){var t;return null===e?new Date(NaN):e?e instanceof Date?e:(t=String(e).match(h))?new Date(t[1],t[2]-1,t[3]):new Date(e):new Date},y=function(){function h(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,h),this.$d=f(e),this.init()}var y,v,m;return y=h,(v=[{key:"init",value:function(){this.$zone=this.$d.getTimezoneOffset()/60,this.$zoneStr=$(String(-1*this.$zone).replace(/^(.)?(\d)/,"$10$200"),5,"+"),this.$y=this.$d.getFullYear(),this.$M=this.$d.getMonth(),this.$D=this.$d.getDate(),this.$W=this.$d.getDay(),this.$H=this.$d.getHours(),this.$m=this.$d.getMinutes(),this.$s=this.$d.getSeconds(),this.$ms=this.$d.getMilliseconds()}},{key:"isValid",value:function(){return!("Invalid Date"===this.$d.toString())}},{key:"isLeapYear",value:function(){return this.$y%4==0&&this.$y%100!=0||this.$y%400==0}},{key:"isSame",value:function(e){return this.valueOf()===e.valueOf()}},{key:"isBefore",value:function(e){return this.valueOf()<e.valueOf()}},{key:"isAfter",value:function(e){return this.valueOf()>e.valueOf()}},{key:"year",value:function(){return this.$y}},{key:"month",value:function(){return this.$M}},{key:"date",value:function(){return this.$D}},{key:"hour",value:function(){return this.$H}},{key:"minute",value:function(){return this.$m}},{key:"second",value:function(){return this.$s}},{key:"millisecond",value:function(){return this.$ms}},{key:"unix",value:function(){return Math.floor(this.valueOf()/1e3)}},{key:"valueOf",value:function(){return this.$d.getTime()}},{key:"startOf",value:function(e,c){var o=this,l=void 0===c||c,$=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o.$y,s=new h(new Date(n,t,e));return l?s:s.endOf(r)},f=function(e,t){return new h(o.toDate()[e].apply(o.toDate(),l?[0,0,0,0].slice(t):[23,59,59,999].slice(t)))};switch(d(e)){case u:return l?$(1,0):$(31,11,this.$y);case a:return l?$(1,this.$M):$(0,this.$M+1,this.$y);case i:return l?$(this.$D-this.$W,this.$M):$(this.$D+(6-this.$W),this.$M,this.$y);case r:case"date":return f("setHours",0);case s:return f("setMinutes",1);case n:return f("setSeconds",2);case t:return f("setMilliseconds",3);default:return this.clone()}}},{key:"endOf",value:function(e){return this.startOf(e,!1)}},{key:"mSet",value:function(e,r){switch(d(e)){case"date":this.$d.setDate(r);break;case a:this.$d.setMonth(r);break;case u:this.$d.setFullYear(r);break;case s:this.$d.setHours(r);break;case n:this.$d.setMinutes(r);break;case t:this.$d.setSeconds(r);break;case"millisecond":this.$d.setMilliseconds(r)}return this.init(),this}},{key:"set",value:function(e,t){return n=t,Number.isNaN(parseFloat(n))||!Number.isFinite(n)?this:this.clone().mSet(e,t);var n}},{key:"add",value:function(e,t){var c,o=t&&1===t.length?t:d(t);if(["M",a].indexOf(o)>-1){var l=this.set("date",1).set(a,this.$M+e);return l=l.set("date",Math.min(this.$D,l.daysInMonth()))}if(["y",u].indexOf(o)>-1)return this.set(u,this.$y+e);switch(o){case"m":case n:c=6e4;break;case"h":case s:c=36e5;break;case"d":case r:c=864e5;break;case"w":case i:c=6048e5;break;default:c=1e3}return new h(this.valueOf()+e*c)}},{key:"subtract",value:function(e,t){return this.add(-1*e,t)}},{key:"format",value:function(e){var t=this;return(e||"YYYY-MM-DDTHH:mm:ssZ").replace(l,function(e){switch(e){case"YY":return String(t.$y).slice(-2);case"YYYY":return String(t.$y);case"M":return String(t.$M+1);case"MM":return $(t.$M+1,2,"0");case"MMM":return o[t.$M].slice(0,3);case"MMMM":return o[t.$M];case"D":return String(t.$D);case"DD":return $(t.$D,2,"0");case"d":return String(t.$W);case"dddd":return c[t.$W];case"H":return String(t.$H);case"HH":return $(t.$H,2,"0");case"h":case"hh":return 0===t.$H?12:$(t.$H<13?t.$H:t.$H-12,"hh"===e?2:1,"0");case"a":return t.$H<12?"am":"pm";case"A":return t.$H<12?"AM":"PM";case"m":return String(t.$m);case"mm":return $(t.$m,2,"0");case"s":return String(t.$s);case"ss":return $(t.$s,2,"0");case"Z":return"".concat(t.$zoneStr.slice(0,-2),":00");default:return t.$zoneStr}})}},{key:"diff",value:function(e,n,s){var c,o,l,$,f,y,v=d(n),m=e instanceof h?e:new h(e),k=this-m,M=(c=this,$=12*((o=m).year()-c.year())+(o.month()-c.month()),f=c.clone().add($,"months"),l=o-f<0?(o-f)/(f-c.clone().add($-1,"months")):(o-f)/(c.clone().add($+1,"months")-f),Number(-($+l)));switch(v){case u:M/=12;break;case a:break;case"quarter":M/=3;break;case i:M=k/6048e5;break;case r:M=k/864e5;break;case t:M=k/1e3;break;default:M=k}return s?M:(y=M)<0?Math.ceil(y)||0:Math.floor(y)}},{key:"daysInMonth",value:function(){return this.endOf(a).$D}},{key:"clone",value:function(){return new h(this)}},{key:"toDate",value:function(){return new Date(this.$d)}},{key:"toArray",value:function(){return[this.$y,this.$M,this.$D,this.$H,this.$m,this.$s,this.$ms]}},{key:"toJSON",value:function(){return this.toISOString()}},{key:"toISOString",value:function(){return this.toDate().toISOString()}},{key:"toObject",value:function(){return{years:this.$y,months:this.$M,date:this.$D,hours:this.$H,minutes:this.$m,seconds:this.$s,milliseconds:this.$ms}}},{key:"toString",value:function(){return this.$d.toUTCString()}}])&&e(y.prototype,v),m&&e(y,m),h}();return function(e){return new y(e)}});
{
"name": "dayjs",
"version": "1.5.16",
"version": "1.5.17",
"description": "",

@@ -5,0 +5,0 @@ "main": "dist/dayjs.min.js",

@@ -22,1 +22,10 @@ export const SECONDS_A_MINUTE = 60

export const DATE = 'date'
export const WEEKS = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
export const MONTHS = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{2})-?(\d{1,2})$/
export const REGEX_FORMAT = /Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}/g

@@ -10,3 +10,3 @@ import * as C from './constant'

// eslint-disable-next-line no-cond-assign
if (reg = String(config).match(/^(\d{4})-?(\d{2})-?(\d{1,2})$/)) {
if (reg = String(config).match(C.REGEX_PARSE)) {
// 2018-08-08 or 20180808

@@ -94,3 +94,4 @@ return new Date(reg[1], reg[2] - 1, reg[3])

startOf(units, isStartOf = true) { // isStartOf -> endOf
startOf(units, startOf) { // startOf -> endOf
const isStartOf = startOf !== undefined ? startOf : true
const unit = Utils.prettyUnit(units)

@@ -212,7 +213,5 @@ const instanceFactory = (d, m, y = this.$y) => {

format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') {
const weeks = 'Sunday.Monday.Tuesday.Wednesday.Thursday.Friday.Saturday'.split('.')
const months = 'January.February.March.April.May.June.July.August.September.October.November.December'.split('.')
return formatStr.replace(/Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g, (match) => {
format(formatStr) {
const str = formatStr || C.FORMAT_DEFAULT
return str.replace(C.REGEX_FORMAT, (match) => {
switch (match) {

@@ -226,27 +225,35 @@ case 'YY':

case 'MM':
return Utils.padStart(String(this.$M + 1), 2, '0')
return Utils.padStart(this.$M + 1, 2, '0')
case 'MMM':
return months[this.$M].slice(0, 3)
return C.MONTHS[this.$M].slice(0, 3)
case 'MMMM':
return months[this.$M]
return C.MONTHS[this.$M]
case 'D':
return String(this.$D)
case 'DD':
return Utils.padStart(String(this.$D), 2, '0')
return Utils.padStart(this.$D, 2, '0')
case 'd':
return String(this.$W)
case 'dddd':
return weeks[this.$W]
return C.WEEKS[this.$W]
case 'H':
return String(this.$H)
case 'HH':
return Utils.padStart(String(this.$H), 2, '0')
return Utils.padStart(this.$H, 2, '0')
case 'h':
case 'hh':
if (this.$H === 0) return 12
return Utils.padStart(this.$H < 13 ? this.$H : this.$H - 12, match === 'hh' ? 2 : 1, '0')
case 'a':
return this.$H < 12 ? 'am' : 'pm'
case 'A':
return this.$H < 12 ? 'AM' : 'PM'
case 'm':
return String(this.$m)
case 'mm':
return Utils.padStart(String(this.$m), 2, '0')
return Utils.padStart(this.$m, 2, '0')
case 's':
return String(this.$s)
case 'ss':
return Utils.padStart(String(this.$s), 2, '0')
return Utils.padStart(this.$s, 2, '0')
case 'Z':

@@ -260,3 +267,3 @@ return `${this.$zoneStr.slice(0, -2)}:00`

diff(input, units, float = false) {
diff(input, units, float) {
const unit = Utils.prettyUnit(units)

@@ -263,0 +270,0 @@ const that = input instanceof Dayjs ? input : new Dayjs(input)

export const padStart = (string, length, pad) => {
if (!string || string.length >= length) return string
return `${Array((length + 1) - string.length).join(pad)}${string}`
const s = String(string)
if (!string || s.length >= length) return string
return `${Array((length + 1) - s.length).join(pad)}${string}`
}

@@ -9,3 +10,3 @@

export const monthDiff = (a, b) => {
// function from moment.js monthDiff
// function from moment.js in order to keep the same result
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())

@@ -22,3 +23,3 @@ const anchor = a.clone().add(wholeMonthDiff, 'months')

}
return Number(-(wholeMonthDiff + adjust)) || 0
return Number(-(wholeMonthDiff + adjust))
}

@@ -25,0 +26,0 @@

@@ -44,2 +44,26 @@ import moment from 'moment'

it('Format Hour h hh 12-hour', () => {
MockDate.set(new Date('2018-05-02T00:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))
MockDate.set(new Date('2018-05-02T01:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))
MockDate.set(new Date('2018-05-02T23:00:00.000'))
expect(dayjs().format('h')).toBe(moment().format('h'))
expect(dayjs().format('hh')).toBe(moment().format('hh'))
})
it('Format meridiens a A am / pm', () => {
MockDate.set(new Date('2018-05-02T01:00:00.000'))
expect(dayjs().format('a')).toBe(moment().format('a'))
expect(dayjs().format('A')).toBe(moment().format('A'))
MockDate.set(new Date('2018-05-02T23:00:00.000'))
expect(dayjs().format('a')).toBe(moment().format('a'))
expect(dayjs().format('A')).toBe(moment().format('A'))
})
it('Format Minute m mm', () => {

@@ -46,0 +70,0 @@ expect(dayjs().format('m')).toBe(moment().format('m'))

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc