date-diff
Version:
DateDiff is a minimalized Javascript date arithmetic extension.
1 lines • 2.8 kB
Source Map (JSON)
{"mappings":"AAWA;IACE,KAAK,EAAE,IAAI,CAAA;IACX,KAAK,EAAE,IAAI,CAAA;IACX,UAAU,EAAE,MAAM,CAAA;gBAEN,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI;IAMpC,IAAI,QAAO,MAAM,CAEhB;IAED,KAAK,QAAO,MAAM,CAEjB;IAED,KAAK,QAAO,MAAM,CAEjB;IAED,OAAO,QAAO,MAAM,CAEnB;IAED,OAAO,QAAO,MAAM,CAEnB;IAED,MAAM,QAAO,MAAM,CAMlB;IAED,KAAK,QAAO,MAAM,CAIjB;IAED,UAAU,SAAU,IAAI,KAAG,IAAI,CAE9B;IAED,SAAS,SAAU,IAAI,KAAG,MAAM,CAE/B;IAED,UAAU,SAAU,IAAI,YAEvB;IAED,WAAW,SAAU,IAAI,KAAG,IAAI,CAE/B;IAED,SAAS,SAAU,IAAI,KAAG,IAAI,CAE7B;CAEF","sources":["src/src/date-diff.ts","src/date-diff.ts"],"sourcesContent":[null,"const divisors = {\n days: 86400000,\n hours: 3600000,\n minutes: 60000,\n seconds: 1000,\n}\n\nconst round = (value: number): number => {\n return parseFloat(value.toFixed(1))\n}\n\nexport default class DateDiff {\n date1: Date\n date2: Date\n difference: number\n\n constructor(date1: Date, date2: Date) {\n this.date1 = date1\n this.date2 = date2\n this.difference = Math.floor(date1.getTime() - date2.getTime())\n }\n\n days = (): number => {\n return round(this.difference / divisors.days)\n }\n\n weeks = (): number => {\n return round(this.days() / 7)\n }\n\n hours = (): number => {\n return round(this.difference / divisors.hours)\n }\n\n minutes = (): number => {\n return round(this.difference / divisors.minutes)\n }\n\n seconds = (): number => {\n return round(this.difference / divisors.seconds)\n }\n\n months = (): number => {\n let ret = (this.date1.getFullYear() - this.date2.getFullYear()) * 12\n ret += this.date1.getMonth() - this.date2.getMonth()\n const endOfMonth = this.endOfMonth(this.date2).getDate()\n ret += (this.date1.getDate() / endOfMonth) - (this.date2.getDate() / endOfMonth)\n return round(ret)\n }\n\n years = (): number => {\n let ret = (this.date1.getFullYear() - this.date2.getFullYear())\n ret += (this.dayOfYear(this.date1) - this.dayOfYear(this.date2)) / this.daysInYear(this.date2)\n return round(ret)\n }\n\n endOfMonth = (date: Date): Date => {\n return new Date(date.getFullYear(), date.getMonth() + 1, 0)\n }\n\n dayOfYear = (date: Date): number => {\n return (date.getTime() - this.beginOfYear(date).getTime()) / divisors.days\n }\n\n daysInYear = (date: Date) => {\n return (this.endOfYear(date).getTime() - this.beginOfYear(date).getTime()) / divisors.days\n }\n\n beginOfYear = (date: Date): Date => {\n return new Date(date.getFullYear(), 0, 0)\n }\n\n endOfYear = (date: Date): Date => {\n return new Date(date.getFullYear() + 1, 0, 0)\n }\n\n}\n\ndeclare global {\n interface DateConstructor {\n diff(date1: Date, date2: Date): DateDiff\n }\n}\n\nDate.diff = (date1: Date, date2: Date) => new DateDiff(date1, date2)\n"],"names":[],"version":3,"file":"date-diff.d.ts.map"}