I'm in struggle trying to convert a month number that i'm getting from API to month name and then render the month name in Datatable.
Example:
month (1) => "January"
month (2) => "February" ...etc
For that, i created this function:
function getMonthName(monthNumber) {
const date = new Date()
date.setMonth(monthNumber - 1)
return date.toLocaleString('en-US', { month: 'long' })
}
It works fine if i provide monthNumber
correctly.
Now i would like to use this function inside month column (so it will show month names instead of month numbers).
What i'm expecting is: getting different month names at each row successively.
What i'm getting is: table always show January only on all its rows.
I reproduced my issue here: Sandbox: https://codesandbox.io/s/hopeful-goldberg-xhqvjm-forked-wwq3m0?file=/src/components/tables/SalesTable/SalesTable.jsx
You can use the js-date helper library. I am using date-fns.
npm install date-fns --save
import {format} from 'date-fns'
// Let say its January
const month = 1
// date can be anything
const date = `1970/${month}/07`
const monthName = format(new Date(date), 'LLLL')