10 examples of 'moment diff' in JavaScript

Every line of 'moment diff' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
155function diff (dt1, dt2) {
156 if (dt2) {
157 return Miment(dt1).valueOf() - Miment(dt2).valueOf()
158 } else {
159 return this.valueOf() - Miment(dt1).valueOf()
160 }
161}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

24get diff() {
25 return nano(this.hr.end) - nano(this.hr.start);
26}
9static diff(start: Date, end: Date, unit: 'h' | 'm' = 'h', offset: number = 0): number {
10 const timeDiff = Math.abs(end.getTime() - start.getTime() + offset);
11 return parseInt((timeDiff / (unit === 'h' ? DateUtil.HOUR : DateUtil.MINUTE)) + '');
12}
18function diff(value, unit = 'day') {
19 const days = dayjs(value).diff(dayjs(), unit)
20 const units = Math.abs(days) === 1 ? 'day' : 'days'
21
22 if (days === 0) {
23 return 'today'
24 }
25
26 return days > 0
27 ? 'in ' + Math.abs(days) + ' ' + units
28 : Math.abs(days) + ' ' + units + ' ago'
29}
53function diffDates(now, then) {
54 const diff = then - now;
55 let remaining = diff;
56 const days = Math.floor(remaining / dayMs);
57 remaining = remaining % dayMs;
58 const hours = Math.floor(remaining / hourMs);
59 remaining = remaining % hourMs;
60 const minutes = Math.floor(remaining / minMs);
61 remaining = remaining % minMs;
62 const seconds = Math.floor(remaining / secMs);
63 remaining = remaining % secMs;
64
65 return {
66 days: addZeros(days),
67 hours: addZeros(hours),
68 minutes: addZeros(minutes),
69 seconds: addZeros(seconds),
70 };
71}
59export function getDiffInMinutes(datetimeObj) {
60 const {period, start, end} = datetimeObj;
61
62 if (start && end) {
63 return moment(end).diff(start, 'minutes');
64 }
65
66 return (
67 parsePeriodToHours(typeof period === 'string' ? period : DEFAULT_STATS_PERIOD) * 60
68 );
69}
128function dateDiff(start, end) {
129 return moment(start).diff(end, 'minutes');
130}
179diff(other) {
180 if (!other) {
181 return NaN;
182 }
183
184 if (typeof this.contentCounter === 'number' && typeof other.contentCounter === 'number') {
185 return this.contentCounter - other.contentCounter;
186 }
187
188 if (typeof this.scriptCounter === 'number' && typeof other.scriptCounter === 'number') {
189 return this.scriptCounter - other.scriptCounter;
190 }
191
192 if (typeof this.backgroundCounter === 'number' && typeof other.backgroundCounter === 'number') {
193 return this.backgroundCounter - other.backgroundCounter;
194 }
195
196 // `time` is always available, however it is less precise
197 return this.time - other.time;
198}
51updateDiff(initialTime) {
52 //console.log("initialTime: " + initialTime);
53 if (initialTime) {
54 if (!environment.demo) this._wasExpired = moment(new Date(initialTime)).diff(moment()) <= 0;
55 else this._wasExpired = moment(new Date(initialTime)).diff(moment(new Date(environment.demoDate))) - moment().diff(this._loadedAt) <= 0;
56 this._notificationSent = false;
57 this._str = undefined;
58 this._ready = this._wasExpired;
59 if (!this._wasExpired && this.state._completed == true) this.state._completed = false;
60 }
61
62 if (!environment.demo) this._diff = initialTime || this.time ? moment.duration(moment(new Date(initialTime || this.time)).diff(moment())) : undefined;
63 else this._diff = initialTime || this.time ? moment.duration(moment(new Date(initialTime || this.time)).diff(moment(new Date(environment.demoDate))) - moment().diff(this._loadedAt)) : undefined;
64}
472export function diffMinutes(a: Date, b: Date): number
473{
474 return diffMilliseconds(a, b) / Constants.MILLIS_IN_MINUTE;
475}

Related snippets