SeanMarc
SeanMarc

Reputation: 139

How can I format the date displayed from an api?

I'm pulling data from an api and want to change the format of the date displayed. What's the best way to achieve this? Do I implement something at the point where I retrieve the data or the point where it is displayed?

import axios from 'axios'
import {popularGamesURL, upcomingGamesURL, newGamesURL} from '../api'


export const loadGames = () => async (dispatch) => {
    const popularData = await axios.get(popularGamesURL());
    const newGamesData = await axios.get(newGamesURL());
    const upcomingData = await axios.get(upcomingGamesURL());
    dispatch({
        type: "FETCH_GAMES",
        payload: {
            popular: popularData.data.date.results,
            upcoming: upcomingData.data.results,
            newGames: newGamesData.data.results,
        }
    });
};

Upvotes: 0

Views: 402

Answers (2)

Vaaneet Kapoor
Vaaneet Kapoor

Reputation: 266

Install moment js in your project. It is one of the best library, giving you the best result. I'd recommend you to format it while displaying. I dont see any point of doing it while retrieving.

moment js will do the date formation. moment(your actual date).format(format you are looking for).

please read api documentation at https://momentjs.com/

Upvotes: 2

Lior Pollak
Lior Pollak

Reputation: 3450

Date formatting is a display stuff, so you should put it in your view.

Check out this beautiful answer for some tips on formatting dates in js: https://stackoverflow.com/a/3552493/938227

Upvotes: 0

Related Questions