Splunk timezone and formatting plugins for moment.js
npm install @splunk/momentA package of Moment and Moment Timezone
plugins for Splunk Enterprise timezones, and formatting for locales with second and millisecond precision.
Install the package:
```
npm install @splunk/moment
Import moment from the Splunk UI package. This provides a Moment class with the timezone and
timezone util plugins.
`js`
import moment from '@splunk/moment';
Create new moment instances in the server timezone and locale.
`js`
const time1 = moment.newSplunkTime({time: 1490500800});
const time2 = moment.newSplunkTime({time: '10/10/2017', format: 'l'});
Manipulate and query times using the Moment API.
`js`
time1.subtract(1, 'day').startOf('day';);
const isBefore = time1.isBefore(time2);
Format Times using second or millisecond precision
`js`
const displayValue = time1.splunkFormat('lls');
If used in an environment without the window.$C properties set by splunkweb, there are
functions to setup and use the plugins with raw timezone data from Splunk Enterprise.
The timezone data can be retrieved from services/search/timeparser/tz. *To access this
service from the client, the endpoint must be exposed in web.conf.*
For example:
``
http://localhost:8000/en-US/splunkd/__raw/services/search/timeparser/tz
Set this data as the default Splunk Enterprise timezone, which can be used for creating and manipulating times.
`js
const splunkTimezoneName = moment.setDefaultSplunkTimezone(zoneData);
const time = moment.newSplunkTime({time: 1490500800}); // uses the default timezone
const nowInTokyo = moment.tz('Asia/Tokyo').locale('ja_JP');
const nowAtServer = nowInTokyo.clone().tz(splunkTimezoneName).locale('en_US');
``