-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the date range format, fix ongoing story
- Loading branch information
1 parent
bd82445
commit 6119f0b
Showing
6 changed files
with
86 additions
and
151 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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,52 @@ | ||
import format from 'date-fns/format' | ||
|
||
/** | ||
* Take two date strings, and return them in human readable date formats for Events | ||
* | ||
* @param {string} startDate | ||
* @param {string} endDate | ||
* @param {string} dateFormat - 'long', 'short', or 'shortWithYear' | ||
* @returns {string} | ||
*/ | ||
|
||
function formatDates(startDate = '', endDate = '', dateFormat = 'long') { | ||
const start = format(new Date(startDate), 'MMMM d, Y') | ||
// console.log(start) | ||
const end = format(new Date(endDate), 'MMMM d, Y') | ||
|
||
// "February 11 2020 – May 31 2021" | ||
let output = `${start} - ${end}` | ||
|
||
if (start === end) { | ||
// Thursday, January 28 | ||
output = format(new Date(startDate), 'MMMM d, Y') | ||
} | ||
|
||
if (!endDate) { | ||
// February 11 2020 | ||
output = start | ||
} | ||
|
||
if (dateFormat === 'short' || dateFormat === 'shortWithYear') { | ||
// "Feb 11 2020" | ||
const day = output.slice(0, 3) | ||
const dateYear = output.split(' ').slice(1).join(' ') | ||
if (endDate && endDate !== startDate) { | ||
// Feb 11 – May 31 | ||
const shortFormatStartDate = format(new Date(startDate), 'MMM d, Y') | ||
const shortFormatEndDate = format(new Date(endDate), 'MMM d, Y') | ||
// if 'shortWithYear' is selected, add the year to the end date | ||
// Feb 11 – May 31 2020 | ||
if (dateFormat === 'shortWithYear') { | ||
const shortFormatEndDateYear = format(new Date(endDate), 'MMM d, Y') | ||
return `${shortFormatStartDate} - ${shortFormatEndDateYear}` | ||
} | ||
return `${shortFormatStartDate} - ${shortFormatEndDate}` | ||
} | ||
return `${day} ${dateYear}` | ||
} | ||
|
||
return output | ||
} | ||
|
||
export default formatDates |