Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

How and I format this date quickly and easily?

Here's what I'm using to get yesterday's date:

const yesterday_raw = new Date(new Date().setDate(new Date().getDate() - 1));
console.log(yesterday_raw);

It works fine, but I need to format it according to YYYY-MM-DD. I've seen a number of approaches on the Internet, but they all look kind of clunky.

I figured I would bring it before the ninjas and see if someone's got an easy way of formatting the above as YYYY-MM-DD.

Thoughts?
Avatar of leakim971
leakim971
Flag of Guadeloupe image

const yesterday_raw = new Date(new Date().setDate(new Date().getDate() - 1));
var formattedDate = new Intl.DateTimeFormat("ko-KR").format(yesterday_raw).replace(/\D/g,"");

User generated image
source : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat
YYYY-MM-DD :

const yesterday_raw = new Date(new Date().setDate(new Date().getDate() - 1));
var formattedDate = new Intl.DateTimeFormat("ko-KR").format(yesterday_raw).replace(/(\.\s|\.)/g,"-").substr(0,10);

Open in new window


User generated image
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Short, simple and sweet!

Thank you!

isn't it amazing how some suggestions online can be so convoluted? One trip to EE and BAM! Problem solved!

Thanks again!