forked from markitondemand/moment-msdate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmoment-msdate.js
76 lines (66 loc) · 2.39 KB
/
moment-msdate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'use strict';
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['moment-timezone'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('moment-timezone'));
} else {
factory(root.moment);
}
}(this, (moment) => {
/**
* Constants
*/
const DAY_MILLISECONDS = 86400000;
const MINUTE_MILLISECONDS = 60000;
const MS_DAY_OFFSET = 25569;
/**
* @description To JavaScript date from OLE Automation date
*
* @param msDate An OLE Automation date. Required.
* @param offsetToUtcInMinutes An offset from the msDate to UTC. If not supplied the offset from the system timezone will be used.
* @returns moment
*/
moment.fromOADate = function(msDate, offsetToUtcInMinutes) {
let jO = new Date(((msDate - MS_DAY_OFFSET) * DAY_MILLISECONDS) + (msDate >= 0.0 ? 0.5 : -0.5));
const tz = isNaN(parseInt(offsetToUtcInMinutes, 10)) ? jO.getTimezoneOffset() : offsetToUtcInMinutes;
jO = new Date((((msDate - MS_DAY_OFFSET) + (tz / (60 * 24))) * DAY_MILLISECONDS) + (msDate >= 0.0 ? 0.5 : -0.5));
return moment(jO);
};
/**
* To OLE Automation date from JavaScript date
*
* @param jsDate A JavaScript date object to convert to OA Date. Defaults to existing moment instance or new Date
* @returns Floating-point number, e.g., 41502.558798240745
*/
moment.fn.toOADate = function(jsDateInput) {
const jsDate = jsDateInput || this._d || new Date();
const timezoneOffset = jsDate.getTimezoneOffset() / (60 * 24);
const msDateObj = (jsDate.getTime() / DAY_MILLISECONDS) + (MS_DAY_OFFSET - timezoneOffset);
return msDateObj;
};
moment.updateLocale('en', {
invalidDate: undefined
});
/**
* Converts an OLE Automation date to a moment (baking in a timezone if one is supplied)
* Returns a UTC Moment object instance
*/
moment.fromOADateWithZone = function(msDate, timeZone) {
const jsTicks = (msDate - MS_DAY_OFFSET) * DAY_MILLISECONDS;
const offset = moment.tz(timeZone).utcOffset() * MINUTE_MILLISECONDS;
if (timeZone) {
return moment.tz(jsTicks - offset, timeZone).utc();
}
return moment.utc(jsTicks);
};
/**
* Converts a moment (with timezone) to an OLE Automation date in UTC
* Returns an OLE Automation date in the form of a double
*/
moment.fn.toOADateWithZone = function() {
const nMsDate = (this.valueOf() / DAY_MILLISECONDS) + MS_DAY_OFFSET;
return nMsDate;
};
return moment;
}));