-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommon.js
48 lines (36 loc) · 1.23 KB
/
common.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
var config = require('./config'),
moment = require('moment');
function getFormattedDate(date, date_format) {
var DATE_FORMAT_INPUT = 'YYYY-MM-DD'; // resume.json standard date format
date_format = date_format || config.date_format; // output format
return moment(date, DATE_FORMAT_INPUT).format(date_format);
}
function humanizeDuration(duration) {
var days,
months = duration.months(),
years = duration.years(),
month_str = months > 1 ? 'months' : 'month',
year_str = years > 1 ? 'years' : 'year';
if ( months && years ) {
return years + ' ' + year_str + ' ' + months + ' ' + month_str;
}
if ( months ) {
return months + ' ' + month_str;
}
if ( years ) {
return years + ' ' + year_str;
}
days = duration.days();
return ( days > 1 ? days + ' days' : days + ' day' );
}
function getDuration(start_date, end_date, humanize) {
var duration;
start_date = new Date(start_date);
end_date = new Date(end_date);
duration = moment.duration(end_date.getTime() - start_date.getTime());
return (humanize ? humanizeDuration(duration) : duration);
}
module.exports = {
getFormattedDate: getFormattedDate,
getDuration: getDuration
};