I am getting value like 202001.
I want to covert it like January_2020.
Is there a way to convert it like this?:
202001->January_2020
i tried below one.
const dateToStr = (input) => {
if (input.length !== 6) {
return "wrong date syntax, use YYYYMM";
}
const parts = input.match(/([0-9]{4})([0-9]{2})/);
if (!parts) {
return "wrong date syntax, use YYYYMM";
}
// NOTE: check syntax of parts
const formatter = new Intl.DateTimeFormat("en-EN", { month: "long" })
.format;
const formatted = formatter(
new Date(Date.UTC(parseInt(parts[1]), parseInt(parts[2]) - 1))
);
return `${formatted}_${parts[1]}`;
};
it is working fine for ios but not working in android.
it showing error like:--
can't find variable:Intl
How can i get this updated value in Javascript without using any library in android and ios both?
No need to use Intl.DateTimeFormat. You may simply use Date class.
const dateToStr = (input) => {
if (input.length !== 6) {
return "wrong date syntax, use YYYYMM";
}
const parts = input.match(/([0-9]{4})([0-9]{2})/);
if (!parts) {
return "wrong date syntax, use YYYYMM";
}
const monthNumber = Number(parts[2]);
if (monthNumber < 1 || monthNumber > 12) {
return "Wrong month number.";
}
const d = new Date();
d.setMonth(monthNumber-1);
const monthName = d.toLocaleString("en-EN", {month: "long"});
return `${monthName}_${parts[1]}`;
};