0

how can I change date format recived from a api when a date comes in UTC format. so that the date will be day/month/year. ?

fetch('https://api.spacexdata.com/v3/launches/next')
.then(result => result.json())
.then((res) => {
    console.log(res.launch_date_local);
})

Looks like I found the solution to this, thanks to all that posted. But this works for me

fetch('https://api.spacexdata.com/v3/launches/next')
.then(result => result.json())
.then((res) => {
    (res.launch_date_local);




            const date = Date.parse(res.launch_date_local);                            
            console.log(new Intl.DateTimeFormat('en-GB').format(date));   
      });



alphadec
  • 21
  • 3
  • 1
    Load it into a `Date` (`new Date(res.launch_date_local)`) and use its accessor methods like `getFullYear`, `getMonth` (note it starts with 0 and not 1!) and `getDate`. See https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date – CherryDT Mar 24 '20 at 12:40
  • UTC is not a format, it's a time standard. If the date is not in one of the two formats recognised by ECMA-262, then using Date.parse is fraught and likely to produce errors in some clients. Also see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Mar 24 '20 at 22:22

1 Answers1

-1

I think you are looking for something like this:

fetch('https://api.spacexdata.com/v3/launches/next')
.then(result => result.json())
.then((res) => {
    const date = new Date(res.launch_date_local);
    const day = date.getDay();
    const month = date.getMonth();
    const year = date.getFullYear();
    console.log(`${day}/${month}/${year}`);
})
  • `date.getDay()` should be `date.getDate()` and `date.getMonth()` should be `date.getMonth() + 1`. Likely the values should also be zero padded to 2 digits. – RobG Mar 24 '20 at 22:16