How to use the duration function from moment-timezone

Find comprehensive JavaScript moment-timezone.duration code examples handpicked from public code repositorys.

moment-timezone.duration returns a duration object based on the input in time units.

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)
fork icon382
star icon0
watch icon53

17
18
19
20
21
22
23
24
25
26
    return `[ Error ] Lirik Lagu ${lagu} tidak di temukan!`
}

const processTime = (timestamp, now) => {
    // timestamp => timestamp when message was received
    return moment.duration(now - moment(timestamp * 1000)).asSeconds()
}
/**
 * is it url?
 * @param  {String} url
fork icon257
star icon114
watch icon14

How does moment-timezone.duration work?

moment-timezone.duration is a function that creates a moment duration object that represents a length of time, such as "3 hours and 30 minutes", with the ability to specify timezone information. It takes an object as a parameter that specifies the duration in terms of years, months, weeks, days, hours, minutes, and seconds, and returns a moment duration object that can be manipulated or formatted as needed.

36
37
38
39
40
41
42
43
44
45
    this.moment._pf = epoch._pf;
    this.epsilon = false;
    return;
}

// A rawDurationString is the result of a moment.duration().toJSON()
// and can be used to construct a moment.duration directly
if (options.rawDurationString) {
    this.duration = moment.duration(options.rawDurationString);
    this.normalize();
fork icon23
star icon292
watch icon19

+ 9 other calls in file

1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117


// Accecpts timestamp as a parameter and returns the difference of the same with current time.
function getTimeDifference(timestamp) {
  const currentTime = Date.now();
  const eventTime = new Date(timestamp);
  const duration = moment.duration(moment(currentTime).diff(moment(eventTime)));
  const days = duration.asDays();
  const years = duration.asYears();
  const months = duration.asMonths();
  const hours = duration.asHours();
fork icon77
star icon54
watch icon20

+ 4 other calls in file

Ai Example

1
2
3
4
5
6
const moment = require("moment-timezone");

// Create a duration object of 2 hours and 30 minutes
const duration = moment.duration({ hours: 2, minutes: 30 });

console.log(duration.asMinutes()); // Output: 150

In this example, we use the moment.duration() method to create a duration object representing 2 hours and 30 minutes. We then use the asMinutes() method to convert the duration to a total number of minutes.

203
204
205
206
207
208
209
210
211
212

/**
 * @param {number} Number/String
 * @return {number} lenght of time duration
 */
const duration = (number) => moment.duration(number);

module.exports = {
    setDefaultTimezone,
    setDefaultFormats,
fork icon12
star icon33
watch icon16

868
869
870
871
872
873
874
875
876
877
},
//---------------------------------------------------------------------
secondsToHoursMinutsSeconds: function (seconds) {

    let ms = seconds*1000;
    let d = moment.duration(ms);
    let time = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

    return time;
},
fork icon1
star icon4
watch icon3

47
48
49
50
51
52
53
54
55
56

let fundsSince = moment(this.history.funds.when).fromNow();
let citizensSince = moment(this.history.citizens.when).fromNow();
let fleetSince = moment(this.history.fleet.when).fromNow();

let duration = moment.duration(moment().diff(moment('2012-10-18')));
let humanizedDuration = humanizeDuration(duration.asMilliseconds(),
                                         {round: true, units: ['mo', 'w']});

let response = 'Star Citizen is currently %s funded (%s since %s).' +
fork icon1
star icon2
watch icon2

79
80
81
82
83
84
85
86
87
88
  range = range.map(x => parseDurationISO8601(x))
  if (range.includes(NaN)) {
    throw new Error(`Unparsable duration range: ${ppJSON(range)}`)
  }
  const val = (range.length > 1) ? randomInt(...range) : range[0]
  return moment.duration(val)
}

function now () {
  return moment()
fork icon38
star icon0
watch icon3

302
303
304
305
306
307
308
309
310
311
            end_time: endTime,
            door_time: startDate.toDate(),
            start_date: startDate.toDate(),
            end_date: endDate.toDate(),
            tour_number: tourNumber,
            duration: moment.duration(duration, 'minutes')
                .toISOString()
        });
    }
});
fork icon2
star icon1
watch icon2

+ 7 other calls in file

106
107
108
109
110
111
112
113
114
115
);
await this.saveState();

if (endingVotes.length > 0) {
  for (const vote of endingVotes) {
    /* const voteAge = moment.duration(now.diff(vote.due));
    // Discard votes we missed for more than 5 minutes
    if (voteAge.asMinutes() >= 5) {
      break;
    } */
fork icon0
star icon0
watch icon0

672
673
674
675
676
677
678
679
680
681
    } : null;
},

secsToDuration: function(secs, opts) {
    let labels = ["hour", "minute", "second"];
    let d = moment.duration(secs, "seconds");
    let hours = parseInt(d.asHours());
    let mins = parseInt(d.minutes());
    let _secs = parseInt(d.seconds());
    let s = [];
fork icon0
star icon0
watch icon1

+ 6 other calls in file

140
141
142
143
144
145
146
147
148
149
  (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(),
    duration: `${slotDuration.hours()}h ${slotDuration.minutes()}m`,
fork icon0
star icon0
watch icon0

89
90
91
92
93
94
95
96
97
98
    return null;
  }

  const toWholeSeconds = Math.abs(Math.floor(this.timeDelta / 1000) * 1000);

  return moment.duration(toWholeSeconds, 'ms').format(this.format, { trim: false });
},
pausedTime() {
  let pausedTime;
  if (this.configuration && this.configuration.pausedTime) {
fork icon0
star icon0
watch icon279