1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | import moment from 'moment'
|
7 |
|
8 | const AM = 'AM'
|
9 | const PM = 'PM'
|
10 | const YEAR = 365
|
11 | const TODAY = 'Today'
|
12 | const ONE_DAY_IN_SECONDS = 86400;
|
13 | const ONE_SECOND = 1000;
|
14 |
|
15 |
|
16 | export function hourTo24Format(hour: string) {
|
17 | return parseInt(moment(hour, ['h A']).format('H'), 10)
|
18 | }
|
19 |
|
20 |
|
21 | export function hourTo12Format(hour: number) {
|
22 | const currDate = new Date()
|
23 | currDate.setHours(hour)
|
24 | return dateTo12Hour(currDate.toISOString())
|
25 | }
|
26 |
|
27 | export const dateTo12Hour = (dateString: string) => {
|
28 | const localDate = new Date(dateString)
|
29 | let hour = localDate.getHours()
|
30 | if (hour === 12) {
|
31 | return ['12', PM]
|
32 | }
|
33 | if (hour === 0) {
|
34 | return ['12', AM]
|
35 | }
|
36 | const afterMidday = hour % 12 === hour
|
37 | hour = afterMidday ? hour : hour % 12
|
38 | const amPm = afterMidday ? AM : PM
|
39 | return [hour.toString(), amPm]
|
40 | }
|
41 |
|
42 | export function increaseDateByDays(date: Date, numOfDays: ?number) {
|
43 | const nextDate = new Date(date.valueOf())
|
44 | nextDate.setDate(nextDate.getDate() + numOfDays)
|
45 | return nextDate
|
46 | }
|
47 |
|
48 | export function pickerDateArray(date: string, daysCount: number = YEAR) {
|
49 | const startDate = date ? new Date(date) : new Date()
|
50 | const arr = []
|
51 |
|
52 | for (let i = 0; i < daysCount; i++) {
|
53 | const ithDateFromStartDate = (Date.parse(startDate) / ONE_SECOND) + (i * ONE_DAY_IN_SECONDS)
|
54 | if (moment.unix(Date.parse(new Date()) / ONE_SECOND).format('MM/DD/YYYY') ===
|
55 | moment.unix(ithDateFromStartDate).format('MM/DD/YYYY')) {
|
56 | arr.push(TODAY)
|
57 | }
|
58 | else {
|
59 | arr.push(
|
60 | formatDatePicker(ithDateFromStartDate)
|
61 | )
|
62 | }
|
63 | }
|
64 | return arr
|
65 | }
|
66 |
|
67 | function formatDatePicker(date: number) {
|
68 | return moment.unix(date).format('ddd MMM D');
|
69 | }
|
70 |
|
71 | export function getHoursArray(format24: boolean) {
|
72 | const hours = format24 ? { min: 0, max: 23 } : { min: 1, max: 12 }
|
73 | const arr = []
|
74 | for (let i = hours.min; i <= hours.max; i++) {
|
75 | arr.push(`00${i}`.slice(-2))
|
76 | }
|
77 | return arr
|
78 | }
|
79 |
|
80 | export function getFiveMinutesArray() {
|
81 | const arr = []
|
82 | arr.push('00')
|
83 | arr.push('05')
|
84 | for (let i = 10; i < 60; i += 5) {
|
85 | arr.push(`${i}`)
|
86 | }
|
87 | return arr
|
88 | }
|
89 |
|
90 | export function getAmArray() {
|
91 | const arr = []
|
92 | arr.push(AM)
|
93 | arr.push(PM)
|
94 | return arr
|
95 | }
|