Reputation: 139
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
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
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