Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.4 to 1.5.5

2

dist/index.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=function(e,t,n){return!e||e.length>=t?e:"".concat(Array(t+1-e.length).join(n)).concat(e)},n=function(e){if(!e)return new Date;if(e instanceof Date)return e;var t=String(e);if(/^(\d){8}$/.test(t)){var n=t.substr(0,4),r=t.substr(4,2),a=t.substr(6,2);return new Date(n,r-1,a)}return new Date(e)},r=function(){function r(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),this.$date=n(e),this.init()}var a,i,s;return a=r,(i=[{key:"init",value:function(){this.timeZone=this.$date.getTimezoneOffset()/60,this.timeZoneString=t(String(-1*this.timeZone).replace(/^(.)?(\d)/,"$10$200"),5,"+"),this.$year=this.$date.getFullYear(),this.$month=this.$date.getMonth(),this.$day=this.$date.getDate(),this.$week=this.$date.getDay(),this.$hour=this.$date.getHours(),this.$minute=this.$date.getMinutes(),this.$second=this.$date.getSeconds()}},{key:"year",value:function(){return this.$year}},{key:"month",value:function(){return this.$month}},{key:"date",value:function(){return this.$day}},{key:"unix",value:function(){return Math.floor(this.valueOf()/1e3)}},{key:"valueOf",value:function(){return this.$date.getTime()}},{key:"toString",value:function(){return this.$date.toUTCString()}},{key:"startOf",value:function(e){switch(e){case"year":return new r(new Date(this.year(),0,1));case"month":return new r(new Date(this.year(),this.month(),1));default:return this}}},{key:"set",value:function(e,t){if(n=t,Number.isNaN(parseFloat(n))||!Number.isFinite(n))return this;var n;switch(e){case"date":this.$date.setDate(t);break;case"month":this.$date.setMonth(t);break;case"year":this.$date.setFullYear(t)}return this.init(),this}},{key:"add",value:function(e,t){if(["M","months"].indexOf(t)>-1){var n=this.clone();return n.set("date",1),n.set("month",this.month()+e),n.set("date",Math.min(this.date(),n.daysInMonth())),n}var a;switch(t){case"m":case"minutes":a=60;break;case"h":case"hours":a=3600;break;case"d":case"days":a=86400;break;case"w":case"weeks":a=604800;break;default:a=1}return new r(1e3*(this.unix()+e*a))}},{key:"subtract",value:function(e,t){return this.add(-1*e,t)}},{key:"format",value:function(){var e=this,n=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DDTHH:mm:ssZ").replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g,function(r){switch(r){case"YY":return String(e.$year).slice(-2);case"YYYY":return String(e.$year);case"M":return String(e.$month+1);case"MM":return t(String(e.$month+1),2,"0");case"D":return String(e.$day);case"DD":return t(String(e.$day),2,"0");case"d":return String(e.$week);case"dddd":return n[e.$week];case"H":return String(e.$hour);case"HH":return t(String(e.$hour),2,"0");case"m":return String(e.$minute);case"mm":return t(String(e.$minute),2,"0");case"s":return String(e.$second);case"ss":return t(String(e.$second),2,"0");case"Z":return"".concat(e.timeZoneString.slice(0,-2),":00");default:return e.timeZoneString}})}},{key:"diff",value:function(e){var t=e instanceof r?e:new r(e);return this.valueOf()-t.valueOf()}},{key:"daysInMonth",value:function(){return new r(new Date(this.year(),this.month()+1,0)).date()}},{key:"clone",value:function(){return new r(this)}},{key:"toDate",value:function(){return new Date(this.$date)}},{key:"toISOString",value:function(){return this.toDate().toISOString()}}])&&e(a.prototype,i),s&&e(a,s),r}();return function(e){return new r(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 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=function(e,t,n){return!e||e.length>=t?e:"".concat(Array(t+1-e.length).join(n)).concat(e)},n=function(e){if(!e)return new Date;if(e instanceof Date)return e;var t=String(e);if(/^(\d){8}$/.test(t)){var n=t.substr(0,4),r=t.substr(4,2),i=t.substr(6,2);return new Date(n,r-1,i)}return new Date(e)},r=function(){function r(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,r),this.$date=n(e),this.init()}var i,a,s;return i=r,(a=[{key:"init",value:function(){this.timeZone=this.$date.getTimezoneOffset()/60,this.timeZoneString=t(String(-1*this.timeZone).replace(/^(.)?(\d)/,"$10$200"),5,"+"),this.$year=this.$date.getFullYear(),this.$month=this.$date.getMonth(),this.$day=this.$date.getDate(),this.$week=this.$date.getDay(),this.$hour=this.$date.getHours(),this.$minute=this.$date.getMinutes(),this.$second=this.$date.getSeconds(),this.$milliseconds=this.$date.getMilliseconds()}},{key:"year",value:function(){return this.$year}},{key:"month",value:function(){return this.$month}},{key:"date",value:function(){return this.$day}},{key:"hour",value:function(){return this.$hour}},{key:"minute",value:function(){return this.$minute}},{key:"second",value:function(){return this.$second}},{key:"millisecond",value:function(){return this.$milliseconds}},{key:"unix",value:function(){return Math.floor(this.valueOf()/1e3)}},{key:"valueOf",value:function(){return this.$date.getTime()}},{key:"startOf",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];switch(e){case"year":return t?new r(new Date(this.year(),0,1)):new r(new Date(this.year(),11,31)).endOf("day");case"month":return t?new r(new Date(this.year(),this.month(),1)):new r(new Date(this.year(),this.month()+1,0)).endOf("day");case"day":return new r(t?this.toDate().setHours(0,0,0,0):this.toDate().setHours(23,59,59,999));default:return this.clone()}}},{key:"endOf",value:function(e){return this.startOf(e,!1)}},{key:"set",value:function(e,t){if(n=t,Number.isNaN(parseFloat(n))||!Number.isFinite(n))return this;var n;switch(e){case"date":this.$date.setDate(t);break;case"month":this.$date.setMonth(t);break;case"year":this.$date.setFullYear(t)}return this.init(),this}},{key:"add",value:function(e,t){if(["M","months"].indexOf(t)>-1){var n=this.clone();return n.set("date",1),n.set("month",this.month()+e),n.set("date",Math.min(this.date(),n.daysInMonth())),n}var i;switch(t){case"m":case"minutes":i=6e4;break;case"h":case"hours":i=36e5;break;case"d":case"days":i=864e5;break;case"w":case"weeks":i=6048e5;break;default:i=1e3}return new r(this.valueOf()+e*i)}},{key:"subtract",value:function(e,t){return this.add(-1*e,t)}},{key:"format",value:function(){var e=this,n=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];return(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"YYYY-MM-DDTHH:mm:ssZ").replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}|Z{1,2}/g,function(r){switch(r){case"YY":return String(e.$year).slice(-2);case"YYYY":return String(e.$year);case"M":return String(e.$month+1);case"MM":return t(String(e.$month+1),2,"0");case"D":return String(e.$day);case"DD":return t(String(e.$day),2,"0");case"d":return String(e.$week);case"dddd":return n[e.$week];case"H":return String(e.$hour);case"HH":return t(String(e.$hour),2,"0");case"m":return String(e.$minute);case"mm":return t(String(e.$minute),2,"0");case"s":return String(e.$second);case"ss":return t(String(e.$second),2,"0");case"Z":return"".concat(e.timeZoneString.slice(0,-2),":00");default:return e.timeZoneString}})}},{key:"diff",value:function(e,t){var n,i,a,s,u,o,h=arguments.length>2&&void 0!==arguments[2]&&arguments[2],c=e instanceof r?e:new r(e),d=this-c,l=(n=this,s=12*((i=c).year()-n.year())+(i.month()-n.month()),u=n.clone().add(s,"months"),a=i-u<0?(i-u)/(u-n.clone().add(s-1,"months")):(i-u)/(n.clone().add(s+1,"months")-u),Number(-(s+a))||0);switch(t){case"years":l/=12;break;case"months":break;case"quarters":l/=3;break;case"weeks":l=d/6048e5;break;case"days":l=d/864e5;break;case"seconds":l=d/1e3;break;default:l=d}return h?l:(o=l)<0?Math.ceil(o)||0:Math.floor(o)}},{key:"daysInMonth",value:function(){return this.endOf("month").date()}},{key:"clone",value:function(){return new r(this)}},{key:"toDate",value:function(){return new Date(this.$date)}},{key:"toArray",value:function(){return[this.year(),this.month(),this.date(),this.hour(),this.minute(),this.second(),this.millisecond()]}},{key:"toJSON",value:function(){return this.toISOString()}},{key:"toISOString",value:function(){return this.toDate().toISOString()}},{key:"toObject",value:function(){return{years:this.year(),months:this.month(),date:this.date(),hours:this.hour(),minutes:this.minute(),seconds:this.second(),milliseconds:this.millisecond()}}},{key:"toString",value:function(){return this.$date.toUTCString()}}])&&e(i.prototype,a),s&&e(i,s),r}();return function(e){return new r(e)}});
{
"name": "dayjs",
"version": "1.5.4",
"version": "1.5.5",
"description": "",

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

@@ -5,1 +5,7 @@ export const SECONDS_A_MINUTE = 60

export const SECONDS_A_WEEK = SECONDS_A_DAY * 7
export const MILLISECONDS_A_SECOND = 1000
export const MILLISECONDS_A_MINUTE = SECONDS_A_MINUTE * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_HOUR = SECONDS_A_HOUR * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_DAY = SECONDS_A_DAY * MILLISECONDS_A_SECOND
export const MILLISECONDS_A_WEEK = SECONDS_A_WEEK * MILLISECONDS_A_SECOND

@@ -33,2 +33,3 @@ import * as Constant from './constant'

this.$second = this.$date.getSeconds()
this.$milliseconds = this.$date.getMilliseconds()
}

@@ -48,2 +49,18 @@

hour() {
return this.$hour
}
minute() {
return this.$minute
}
second() {
return this.$second
}
millisecond() {
return this.$milliseconds
}
unix() {

@@ -58,17 +75,28 @@ return Math.floor(this.valueOf() / 1000)

toString() {
return this.$date.toUTCString()
}
startOf(arg) {
startOf(arg, isStartOf = true) {
switch (arg) {
case 'year':
return new Dayjs(new Date(this.year(), 0, 1))
if (isStartOf) {
return new Dayjs(new Date(this.year(), 0, 1))
}
return new Dayjs(new Date(this.year(), 11, 31)).endOf('day')
case 'month':
return new Dayjs(new Date(this.year(), this.month(), 1))
if (isStartOf) {
return new Dayjs(new Date(this.year(), this.month(), 1))
}
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).endOf('day')
case 'day':
if (isStartOf) {
return new Dayjs(this.toDate().setHours(0, 0, 0, 0))
}
return new Dayjs(this.toDate().setHours(23, 59, 59, 999))
default:
return this
return this.clone()
}
}
endOf(arg) {
return this.startOf(arg, false)
}
set(string, int) {

@@ -105,21 +133,21 @@ if (!Utils.isNumber(int)) return this

case 'minutes':
step = Constant.SECONDS_A_MINUTE
step = Constant.MILLISECONDS_A_MINUTE
break
case 'h':
case 'hours':
step = Constant.SECONDS_A_HOUR
step = Constant.MILLISECONDS_A_HOUR
break
case 'd':
case 'days':
step = Constant.SECONDS_A_DAY
step = Constant.MILLISECONDS_A_DAY
break
case 'w':
case 'weeks':
step = Constant.SECONDS_A_WEEK
step = Constant.MILLISECONDS_A_WEEK
break
default: // s seconds
step = 1
step = Constant.MILLISECONDS_A_SECOND
}
const nextTimeStamp = this.unix() + (number * step)
return new Dayjs(nextTimeStamp * 1000)
const nextTimeStamp = this.valueOf() + (number * step)
return new Dayjs(nextTimeStamp)
}

@@ -172,9 +200,32 @@

diff(otherDate) {
diff(otherDate, unit, float = false) {
const other = otherDate instanceof Dayjs ? otherDate : new Dayjs(otherDate)
return this.valueOf() - other.valueOf()
const diff = this - other
let result = Utils.monthDiff(this, other)
switch (unit) {
case 'years':
result /= 12
break
case 'months':
break
case 'quarters':
result /= 3
break
case 'weeks':
result = diff / Constant.MILLISECONDS_A_WEEK
break
case 'days':
result = diff / Constant.MILLISECONDS_A_DAY
break
case 'seconds':
result = diff / Constant.MILLISECONDS_A_SECOND
break
default: // milliseconds
result = diff
}
return float ? result : Utils.absFloor(result)
}
daysInMonth() {
return new Dayjs(new Date(this.year(), this.month() + 1, 0)).date()
return this.endOf('month').date()
}

@@ -190,7 +241,39 @@

toArray() {
return [
this.year(),
this.month(),
this.date(),
this.hour(),
this.minute(),
this.second(),
this.millisecond()
]
}
toJSON() {
return this.toISOString()
}
toISOString() {
return this.toDate().toISOString()
}
toObject() {
return {
years: this.year(),
months: this.month(),
date: this.date(),
hours: this.hour(),
minutes: this.minute(),
seconds: this.second(),
milliseconds: this.millisecond()
}
}
toString() {
return this.$date.toUTCString()
}
}
export default config => (new Dayjs(config))

@@ -8,1 +8,19 @@ export const padStart = (string, length, pad) => {

export const monthDiff = (a, b) => {
// function from moment.js monthDiff
const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month())
const anchor = a.clone().add(wholeMonthDiff, 'months')
let anchor2
let adjust
if (b - anchor < 0) {
anchor2 = a.clone().add(wholeMonthDiff - 1, 'months')
adjust = (b - anchor) / (anchor - anchor2)
} else {
anchor2 = a.clone().add(wholeMonthDiff + 1, 'months')
adjust = (b - anchor) / (anchor2 - anchor)
}
return Number(-(wholeMonthDiff + adjust)) || 0
}
export const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))

@@ -62,11 +62,52 @@ import moment from 'moment'

it('Difference', () => {
const dateString = '20110101'
describe('Difference', () => {
it('empty -> default milliseconds', () => {
const dateString = '20110101'
const dayjsA = dayjs()
const dayjsB = dayjs(dateString)
const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})
const dayjsA = dayjs()
const dayjsB = dayjs(dateString)
it('diff -> none dayjs object', () => {
const dateString = '2013-02-08'
const dayjsA = dayjs()
const dayjsB = new Date(dateString)
const momentA = moment()
const momentB = new Date(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
})
const momentA = moment()
const momentB = moment(dateString)
expect(dayjsA.diff(dayjsB)).toBe(momentA.diff(momentB))
it('diff -> in seconds, days, weeks, months, quarters, years ', () => {
const dayjsA = dayjs()
const dayjsB = dayjs().add(1000, 'days')
const dayjsC = dayjs().subtract(1000, 'days')
const momentA = moment()
const momentB = moment().add(1000, 'days')
const momentC = moment().subtract(1000, 'days')
const units = ['seconds', 'days', 'weeks', 'months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
it('Special diff in month according to moment.js', () => {
const dayjsA = dayjs('20160115')
const dayjsB = dayjs('20160215')
const dayjsC = dayjs('20170115')
const momentA = moment('20160115')
const momentB = moment('20160215')
const momentC = moment('20170115')
const units = ['months', 'quarters', 'years']
units.forEach((unit) => {
expect(dayjsA.diff(dayjsB, unit)).toBe(momentA.diff(momentB, unit))
expect(dayjsA.diff(dayjsB, unit, true)).toBe(momentA.diff(momentB, unit, true))
expect(dayjsA.diff(dayjsC, unit)).toBe(momentA.diff(momentC, unit))
expect(dayjsA.diff(dayjsC, unit, true)).toBe(momentA.diff(momentC, unit, true))
})
})
})

@@ -84,5 +125,6 @@

expect(dayjs().daysInMonth()).toBe(moment().daysInMonth())
expect(dayjs('20140201').daysInMonth()).toBe(moment('20140201').daysInMonth())
})
it('As Javascript Date', () => {
it('As Javascript Date -> toDate', () => {
const base = dayjs()

@@ -98,5 +140,16 @@ const momentBase = moment()

it('As ISO 8601 String e.g. 2013-02-04T22:44:30.652Z', () => {
it('As Array -> toArray', () => {
expect(dayjs().toArray()).toEqual(moment().toArray())
})
it('As JSON -> toJSON', () => {
expect(dayjs().toJSON()).toBe(moment().toJSON())
})
it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
expect(dayjs().toISOString()).toBe(moment().toISOString())
})
it('As Object -> toObject', () => {
expect(dayjs().toObject()).toEqual(moment().toObject())
})

@@ -13,12 +13,20 @@ import moment from 'moment'

it('StartOf Year', () => {
it('StartOf EndOf Year', () => {
expect(dayjs().startOf('year').unix()).toBe(moment().startOf('year').unix())
expect(dayjs().endOf('year').unix()).toBe(moment().endOf('year').unix())
})
it('StartOf Month', () => {
it('StartOf EndOf Month', () => {
expect(dayjs().startOf('month').unix()).toBe(moment().startOf('month').unix())
expect(dayjs().endOf('month').unix()).toBe(moment().endOf('month').unix())
})
it('StartOf Other', () => {
it('StartOf EndOf Day', () => {
expect(dayjs().startOf('day').unix()).toBe(moment().startOf('day').unix())
expect(dayjs().endOf('day').unix()).toBe(moment().endOf('day').unix())
})
it('StartOf EndOf Other -> no change', () => {
expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix())
expect(dayjs().endOf('otherString').unix()).toBe(moment().endOf('otherString').unix())
})

@@ -25,0 +33,0 @@

@@ -12,17 +12,23 @@ import moment from 'moment'

})
describe('Parse', () => {
it('Now', () => {
expect(dayjs().unix()).toBe(moment().unix())
})
it('Now', () => {
expect(dayjs().unix()).toBe(moment().unix())
})
it('String 20130208', () => {
expect(dayjs('20130208').unix()).toBe(moment('20130208').unix())
})
it('String 20130208', () => {
expect(dayjs('20130208').unix()).toBe(moment('20130208').unix())
})
it('String ISO 8601 date, time and zone ', () => {
const time = '2018-04-04T16:00:00.000Z'
expect(dayjs(time).unix()).toBe(moment(time).unix())
})
it('String ISO 8601 date, time and zone ', () => {
const time = '2018-04-04T16:00:00.000Z'
expect(dayjs(time).unix()).toBe(moment(time).unix())
it('String Other', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
})
})
it('String timestamp 1523520536000 ms', () => {
it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
const timestamp = 1523520536000

@@ -32,7 +38,2 @@ expect(dayjs(timestamp).unix()).toBe(moment(timestamp).unix())

it('String Other', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
})
it('Clone not affect each other', () => {

@@ -39,0 +40,0 @@ const base = dayjs(20170101)

Sorry, the diff of this file is not supported yet

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