Hello,
I get the current date through this function
String(new Date).substring(4, 15)
Is there a way to separate the month and day with a comma?
Thanks
Hello,
I get the current date through this function
String(new Date).substring(4, 15)
Is there a way to separate the month and day with a comma?
Thanks
Between month and day or did you mean between day and year? Usually we represent dates as May 16, 2021
. Just want to make sure I understand the question.
If you want it between the day and year, you can do it simply by using a Date
object’s toLocaleDateString()
but specifying the options…
let dt = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
console.log(dt);
If you really want the comma between the month and day, you might just need to build the date using more substring manipulation.
Here is how I might expand that out, so that the information is easily configurable and grouped together.
const localeInfo = {
name: "en-US",
options: {
year: "numeric",
month: "long",
day: "numeric"
}
};
function formatDate(date, localeInfo) {
var locale = localeInfo.name;
var options = localeInfo.options;
return date.toLocaleDateString(locale, options);
}
const date = new Date();
const formattedDate = formatDate(date, localeInfo);
console.log(formattedDate);
Thank you so much! This was perfect
Thank you!!
/me glares at one-liners of code.
Thanks, you’re most welcome!
Only in the USA.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.