-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from tim-s-ccs/make-some-updates-around-dates
Add some new features
- Loading branch information
Showing
5 changed files
with
62 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const addLeadingZeros = (num: number, length: number): string => { | ||
return ('0'.repeat(length) + num).slice(-length) | ||
} | ||
|
||
export default addLeadingZeros |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import addLeadingZeros from './addLeadingZeros' | ||
|
||
const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', | ||
'July', 'August', 'September', 'October', 'November', 'December' | ||
] | ||
|
||
const hourToTwelveHour = (hour: number): number => { | ||
const twelveHour = hour % 12 | ||
|
||
if (twelveHour === 0) { | ||
return 12 | ||
} else { | ||
return twelveHour | ||
} | ||
} | ||
|
||
const amOrPm = (hour: number): string => { | ||
if (hour <= 12) { | ||
return 'am' | ||
} else { | ||
return 'pm' | ||
} | ||
} | ||
|
||
const datePart = (date: Date): string => { | ||
return `${date.getDate()} ${monthNames[date.getMonth()]} ${date.getFullYear()}` | ||
} | ||
|
||
const timePart = (date: Date): string => { | ||
return `${hourToTwelveHour(date.getHours())}:${addLeadingZeros(date.getMinutes(), 2)}${amOrPm(date.getHours())}` | ||
} | ||
|
||
const formatDate = (date: Date, withTime: false): string => { | ||
const dateString = datePart(date) | ||
|
||
if (withTime) { | ||
return `${dateString}, ${timePart(date)}` | ||
} else { | ||
return dateString | ||
} | ||
} | ||
|
||
export default formatDate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
const getUpdatedAt = (): string => { | ||
const getCurrentDate = (): string => { | ||
let currentDate = new Date() | ||
const offset = currentDate.getTimezoneOffset() | ||
currentDate = new Date(currentDate.getTime() - (offset*60*1000)) | ||
return currentDate.toISOString().split('T')[0] | ||
return currentDate.toISOString() | ||
} | ||
|
||
export default getUpdatedAt | ||
export default getCurrentDate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
import addLeadingZeros from './addLeadingZeros' | ||
import cast from './cast' | ||
import getUpdatedAt from './getUpdatedAt' | ||
import formatDate from './formatDate' | ||
import getCurrentDate from './getCurrentDate' | ||
import numberToCurrency from './numberToCurrency' | ||
import pluralise from './pluralise' | ||
|
||
export default { | ||
addLeadingZeros: addLeadingZeros, | ||
cast: cast, | ||
getUpdatedAt: getUpdatedAt, | ||
formatDate: formatDate, | ||
getCurrentDate: getCurrentDate, | ||
pluralise: pluralise, | ||
numberToCurrency: numberToCurrency | ||
} |