Skip to content

Instantly share code, notes, and snippets.

@cvetkovskin
Created August 11, 2021 08:33
Show Gist options
  • Save cvetkovskin/ac24ae5034c4d59070b0f6060b4ad35a to your computer and use it in GitHub Desktop.
Save cvetkovskin/ac24ae5034c4d59070b0f6060b4ad35a to your computer and use it in GitHub Desktop.
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