Home > Blockchain >  How to convert format date to dd/mm
How to convert format date to dd/mm

Time:05-18

Heloo, i want to convert format date 2022-04-09 08:00:33 to format 9 April. i was trying to convert it, but the format still wrong, thanks for help me before

#solved

CodePudding user response:

When it comes to date manipulation in JavaScript, there are a lot of third-party libraries available. Some of the most popular options are Moment.js and Day.js. When it comes to formatting a date into a custom format, these libraries are really easy to use.

Example:

moment('2022-04-09 08:00:33','YYYY-MM-DD HH:mm:ss').format('D MMMM')

converts:

2022-04-09 08:00:33 to 9 April

You can visit the link to see available formats.

Or You can use a simple solution that involves native date class:

const date = new Date().toLocaleDateString('en-gb', {
    day: 'numeric',
    month: 'long',
  });
  console.log(date)

but for this input date should be in proper date format i.e., ISO String of timestamp in milliseconds.

The arrangement of day and month will be according to locale you use.

CodePudding user response:

export const formatDate = (date) => {
  const newDate = new Date(date)
  const month = newDate.toLocaleString('default', { month: 'long' })
  const day = newDate.getDate()
  const formated = `${day} ${month}`
  return formated
}
  • Related