UNPKG

2.42 kBJavaScriptView Raw
1/**
2 * @prettier
3 * @flow
4 * */
5
6import moment from 'moment'
7
8const AM = 'AM'
9const PM = 'PM'
10const YEAR = 365
11const TODAY = 'Today'
12const ONE_DAY_IN_SECONDS = 86400;
13const ONE_SECOND = 1000;
14
15// it takes in format '12 AM' and return 24 format
16export function hourTo24Format(hour: string) {
17 return parseInt(moment(hour, ['h A']).format('H'), 10)
18}
19
20// it takes in format 23 and return [11,'PM'] format
21export function hourTo12Format(hour: number) {
22 const currDate = new Date()
23 currDate.setHours(hour)
24 return dateTo12Hour(currDate.toISOString())
25}
26
27export 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
42export function increaseDateByDays(date: Date, numOfDays: ?number) {
43 const nextDate = new Date(date.valueOf())
44 nextDate.setDate(nextDate.getDate() + numOfDays)
45 return nextDate
46}
47
48export 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
67function formatDatePicker(date: number) {
68 return moment.unix(date).format('ddd MMM D');
69}
70
71export 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
80export 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
90export function getAmArray() {
91 const arr = []
92 arr.push(AM)
93 arr.push(PM)
94 return arr
95}