1

I have this code that shows the date as "Mar 25 2022" for example. How can modify the code to display a comma between the day number and year? Like this: "Mar 25, 2022"

var d = (new Date()).toString().split(' ').splice(1,3).join(' ');
document.write(d)
mhweb
  • 59
  • 6
  • 1
    Is there a specific language using this date structure? – Teemu Mar 25 '22 at 10:34
  • Ref :https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – John Lobo Mar 25 '22 at 10:39
  • javascript has its own function to format dates according to languages and countries. Standards and practices must be respected, in order to avoid any ambiguity of interpretation – Mister Jojo Mar 25 '22 at 10:43

1 Answers1

2

You didn't show us the code which is generating this date, and if possible you should fix the format mask there to get the output you want. Assuming you can't do that, here is one terse way of adding the comma:

var date = "Mar 25 2022";
date = date.replace(/(?<=\d) /, ", ");
console.log(date);
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360