-3

We have an API that accepts a date string as argument in the format:

2020-11-30

When we have a date object it's very easy in PowerShell to achieve the correct format:

(Get-Date).ToString('yyyy-MM-dd')

Doing this in javascript is a bit more difficult

date.toISOString() // 2020-10-28T00:00:00.000Z

How can to get rid of the time part in the string? Is there a better way to format a date in a custom matter?

DarkLite1
  • 9,743
  • 30
  • 88
  • 160

2 Answers2

-1

Using the moment library you can format the date in many different ways, like here for example:

moment().format('YYYY MM DD');

Moment Website

Nicholas Hamilton
  • 8,709
  • 5
  • 47
  • 75
-2

If you just want the date part, date.toDateString() is the answer for you; if you're looking for some common solution, maybe you should try some libs like momentjs or dayjs(more lightweight).

Jerry
  • 168
  • 8