forked from HospitalRun/hospitalrun-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdob-days.js
80 lines (75 loc) · 2.11 KB
/
dob-days.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Ember from 'ember';
import moment from 'moment';
export default Ember.Mixin.create({
convertDOBToText(birthDate, shortFormat, omitDays) {
let today = new Date();
let years = 0;
let months = 0;
let days = 0;
if (birthDate) {
if (birthDate.getFullYear === undefined) {
birthDate = moment(birthDate, 'l').toDate();
}
if (birthDate.getFullYear !== undefined) {
years = today.getFullYear() - birthDate.getFullYear();
if (today.getMonth() < birthDate.getMonth()
|| (today.getMonth() === birthDate.getMonth()
&& today.getDate() < birthDate.getDate())) {
years--;
}
}
if (birthDate.getMonth) {
months = today.getMonth() - birthDate.getMonth();
days = today.getDate() - birthDate.getDate();
if (months <= 0) {
if (days < 0) {
months += 11;
} else if (months < 0) {
months += 12;
}
} else {
if (days < 0) {
months = months - 1;
}
}
}
if (birthDate.getDate) {
days = today.getDate() - birthDate.getDate();
if (days < 0) {
days += 30;
}
}
}
let formatString = '';
let options = {
years,
days,
months
};
let i18n = this.get('i18n');
if (shortFormat) {
if (years > 0) {
formatString = i18n.t('dates.short', options);
} else {
formatString = i18n.t('dates.shortOmitYears', options);
}
} else if (omitDays) {
if (years > 1) {
formatString = i18n.t('dates.longOmitDaysPlural', options);
} else if (years === 1) {
formatString = i18n.t('dates.longOmitDays', options);
} else {
formatString = i18n.t('dates.longOmitDaysYears', options);
}
} else {
if (years > 1) {
formatString = i18n.t('dates.longPlural', options);
} else if (years === 1) {
formatString = i18n.t('dates.long', options);
} else {
formatString = i18n.t('dates.longOmitYears', options);
}
}
return formatString;
}
});