1

I'm using @nuxtjs/axios in my nuxt.js project. I'm fetching a content_type from my API, but axios formats the date into a ISO 8601 format. Is there any way you can modify this in the nuxt.config.js?

like:

axios: {
 data: 'yyyy/dd/mm'
}

any solution?

I don't want to use the moment library

Rahul
  • 231
  • 1
  • 3
  • 14
Eric
  • 361
  • 6
  • 25
  • 1
    I think you can’t change the date format, since this is a string generated by the server. You can use the middleware to convert the data format during processing. – user2042869 Jan 31 '20 at 14:59
  • "I dont want to use the moment library" I mean, that's up to you, but it's considered the gold standard in JS date work for a reason. Axios isn't doing anything here beyond converting the Date object into a string. Have you considered fixing your API to use the ISO 8601 standard instead of a non-standard date format? – ceejayoz Jan 31 '20 at 15:11

1 Answers1

3

It's really not about Axios. Axios is just using JSON.stringify on a request object and JSON.stringify converts dates into ISO 8601 format. If you want different format, don't pass a Date - format it yourself and pass a string to Axios instead of Date

Basic Date formatting is easy, you don't need Moment.js. For more advanced formating, date-fns is much better/smaller (with support for tree-shaking)

myDateFormat = function(d) {
  return d.getFullYear() + "/" + ("0" + d.getDate()).slice(-2) + "/" + ("0"+(d.getMonth()+1)).slice(-2);
}

var d1 = new Date(Date.now())

console.log(d1)
console.log(myDateFormat(d1))

Also consider changing your API to use more standard date format....

Michal Levý
  • 33,064
  • 4
  • 68
  • 86