How to use moment-timezone

Comprehensive moment-timezone code examples:

How to use moment-timezone.min:

139
140
141
142
143
144
145
146
147
148
.filter(
  (slot) => slot.start >= bufferStart && slot.end <= bufferEnd
)
.map((slot) => {
  const startSlot = moment.max(slot.start, bufferStart);
  const endSlot = moment.min(slot.end, bufferEnd);
  const slotDuration = moment.duration(endSlot.diff(startSlot));
  const slotObj = {
    start: startSlot.toString(),
    end: endSlot.toString(),

How to use moment-timezone.max:

138
139
140
141
142
143
144
145
146
147
const hourSlots = slots
  .filter(
    (slot) => slot.start >= bufferStart && slot.end <= bufferEnd
  )
  .map((slot) => {
    const startSlot = moment.max(slot.start, bufferStart);
    const endSlot = moment.min(slot.end, bufferEnd);
    const slotDuration = moment.duration(endSlot.diff(startSlot));
    const slotObj = {
      start: startSlot.toString(),

How to use moment-timezone.preciseDiff:

89
90
91
92
93
94
95
96
97
98
data.pokestopName = data.name
data.url = data.url || this.config.fallbacks?.pokestopUrl
data.pokestopUrl = data.url

const lureExpiration = data.lure_expiration
data.tth = moment.preciseDiff(Date.now(), lureExpiration * 1000, true)
const disappearTime = moment(lureExpiration * 1000).tz(geoTz.find(data.latitude, data.longitude)[0].toString())
data.disappearTime = disappearTime.format(this.config.locale.time)
data.applemap = data.appleMapUrl // deprecated
data.mapurl = data.googleMapUrl // deprecated

How to use moment-timezone.fromNow:

47
48
49
50
51
52
53
54
55
56
}

function relativeDateHelper(date) {
  const { config } = this;
  const moment = getMoment(date, getLanguage(this), config.timezone);
  return moment.fromNow();
}

function timeTagHelper(date, format) {
  const { config } = this;

How to use moment-timezone.format:

38
39
40
41
42
43
44
45
46
47
}

function fullDateHelper(date, format) {
  if (format) {
    const moment = getMoment(date, getLanguage(this), this.config.timezone);
    return moment.format(format);
  }

  return `${this.date(date)} ${this.time(date)}`;
}

How to use moment-timezone.relativeTimeThreshold:

29
30
31
32
33
34
35
36
37
38
        if (typeof str !== 'string') return '';
        return str.charAt(0).toUpperCase() + str.slice(1);
};

exports.momentThreshold = () => {
        moment.relativeTimeThreshold('s', 60);
        moment.relativeTimeThreshold('ss', 0);
        moment.relativeTimeThreshold('m', 60);
        moment.relativeTimeThreshold('h', 24);
        moment.relativeTimeThreshold('d', 31);

How to use moment-timezone.suppressDeprecationWarnings:

36
37
38
39
40
41
42
43
44
45
46
47
 *
 * @class InvalidFormatter
 */
class InvalidFormatter extends LocalizationError {}


const warnings = Moment.suppressDeprecationWarnings


const errors = {
  INVALID_TIMESTAMP: InvalidTimestamp,
  INVALID_FORMATTER: InvalidFormatter,

How to use moment-timezone.isMoment:

47
48
49
50
51
52
53
54
55
56
  return path.join(dir, base.slice(0, pos) + str + base.slice(pos))
}

function closestYear (unknownDate, referenceDate) {
  // Convert to moment objects
  unknownDate = moment.isMoment(unknownDate) ? unknownDate : moment.utc(unknownDate)
  referenceDate = moment.isMoment(referenceDate) ? referenceDate : moment.utc(referenceDate)

  // Set the year of unknownDate that places it closest to referenceDate
  const year = referenceDate.year()

How to use moment-timezone.locale:

How to use moment-timezone.unix:

618
619
620
621
622
623
624
625
626
627

let pastTimeDifference;
let futureTimeDifference;

const currentTime = moment.unix(moment().format('X'));
const time = moment.unix(moment(formattedVal).format('X'));

switch (allowedPastTimeUnit) {
  case 'seconds':
    pastTimeDifference = Math.ceil(moment.duration(currentTime.diff(time)).asSeconds());

How to use moment-timezone.parseZone:

37
38
39
40
41
42
43
44
45
46

const isNumber = args => args.reduce((prev, next) => prev && typeof next === 'number', true);

const parseTime = (str) => {
  try {
    const t = moment.parseZone(str, time_ISO_8601);
    if (t.isValid()) {
      return t;
    }
    throw new Error('Invalid ISO_8601 format time. This is usually caused by an inappropriate format. Please check the input format.');

How to use moment-timezone.duration:

76
77
78
79
80
81
82
83
84
85

var ticketAvgTotal = _($ticketAvg).reduce(function (m, x) {
  return m + x
}, 0)

var tvt = moment.duration(Math.round(ticketAvgTotal / _.size($ticketAvg)), 'seconds').asHours()
cbObj.avgResponse = Math.floor(tvt)

if (_.isFunction(callback)) {
  return callback(cbObj)

How to use moment-timezone.utc:

How to use moment-timezone.tz:

239
240
241
242
243
244
245
246
247
248
249
250
}


// Get the offset value in Seconds for timezone


const getOffsetInSec = (value) => {
  const name = moment.tz.zone(value);
  if (name) {
    const x = moment().tz(value).format('Z');
    const split = x.split(':');
    const hour = Number(split[0]);