How to use the formatDuration function from date-fns

Find comprehensive JavaScript date-fns.formatDuration code examples handpicked from public code repositorys.

209
210
211
212
213
214
215
216
217
if (quota <= 0) {
  const duration = intervalToDuration({
    start: new Date().getTime(),
    end: tomorrow,
  });
  const formattedDuration = formatDuration(duration, {
    delimiter: ', ',
    format: ['hours', 'minutes'],
  });
fork icon0
star icon1
watch icon2

14
15
16
17
18
19
20
21
22
23
if (allowSeconds) {
  defOpts.push('seconds');
}

const options = { format: defOpts };
const fmt = () => dfns.formatDuration(dfns.intervalToDuration({ start, end }), options);
let dur = fmt();

if (!dur) {
  options.format.push('seconds');
fork icon1
star icon13
watch icon3

22
23
24
25
26
27
28
29
30
  }
  return stdout;
}

function durationToString(durationInSeconds) {
  return formatDuration(
    intervalToDuration({start: 0, end: durationInSeconds * 1000})
  );
}
fork icon0
star icon1
watch icon2

0
1
2
3
4
5
6
7
8
9
10
11
const { formatDuration, intervalToDuration, format } = require("date-fns");
const vi = require("date-fns/locale/vi");


function getReadableMilis(start, end) {
	const duration = intervalToDuration({ start, end });
	return formatDuration(duration, { locale: vi });
}


function getCurrentTime() {
	return format(new Date(), "dd-MM-yyyy", { locale: vi });
fork icon0
star icon0
watch icon0

7
8
9
10
11
12
13
14
  } else {
    const duration = fns.intervalToDuration({
      start: new Date(0),
      end: new Date(durationInMillis)
    });
    return fns.formatDuration(duration).replace(/ hours?/, 'h').replace(/ minutes?/, 'm').replace(/ seconds?/, 's');
  }
};
fork icon0
star icon0
watch icon8

149
150
151
152
153
154
155
156
157
158
    lastEntry['interval']['minutes'] * 60 +
    lastEntry['interval']['hours'] * 60 * 60 +
    lastEntry['interval']['days'] * 60 * 60 * 24;
  formatDuration(lastEntry['interval'], { format: ['seconds'] });

  lastEntry['duration'] = formatDuration(lastEntry['interval']);
}

powerSwitchLog.push({
  on: getPowerRelayState(),
fork icon0
star icon0
watch icon2

+ 3 other calls in file

Other functions in date-fns

Sorted by popularity

function icon

date-fns.format is the most popular function in date-fns (3838 examples)