Created
August 11, 2021 08:33
-
-
Save cvetkovskin/ac24ae5034c4d59070b0f6060b4ad35a to your computer and use it in GitHub Desktop.
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
import moment from 'moment' | |
/** | |
* Format a date into a humanized time sentence if it's in the same | |
* day or otherwise display a date in the YYYY/MM/DD format | |
* | |
* @param {String} dateString | |
* @returns {String} | |
*/ | |
export const formatDate = (dateString: string): string => { | |
const date = moment(dateString, 'YYYYMMDDHHmmss') | |
if (moment().isSame(date, 'day')) { | |
return moment.duration(date.diff(moment(), 'milliseconds')).humanize(true) | |
} else { | |
return date.format('YYYY/MM/DD') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment