Skip to content

Instantly share code, notes, and snippets.

@SebastianUdden
Created February 25, 2022 10:38
Show Gist options
  • Select an option

  • Save SebastianUdden/e3e63c720e51d85327e8d5971e7c30e5 to your computer and use it in GitHub Desktop.

Select an option

Save SebastianUdden/e3e63c720e51d85327e8d5971e7c30e5 to your computer and use it in GitHub Desktop.
const getLeadingZero = number => (number < 10 ? `0${number}` : number)
export const formatDate = dateString => {
const dateTime = new Date(dateString)
const year = dateTime.getFullYear()
const month = getLeadingZero(dateTime.getMonth() + 1)
const day = getLeadingZero(dateTime.getDate())
const hours = getLeadingZero(dateTime.getHours())
const minutes = getLeadingZero(dateTime.getMinutes())
return { date: `${year}-${month}-${day}`, time: `${hours}:${minutes}` }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment