[Vuejs]-Convert numeric timestamp value to `MMM YYYY` format

1👍

✅

You have 2 ways of solving you issue. You either change the array that comes from the api into the format you want, or you do a map of your api array on the frontend side and format each date.

In my example I’ll use luxon:

const result = [
                 1600934100.0,
                 1602009600.0,
                 1602747060.0,
                 1603050158.390939,
                 1603305573.992575
             ].map(date => DateTime.fromMillis(date * 1000).toFormat("MMM yyyy"));

This basically will create a new array as the following

 [ "Sep 2020", "Oct 2020", "Oct 2020", "Oct 2020", "Oct 2020" ]

1👍

You may turn your numeric values into Date and cast that into properly formatted (Date.prototype.toLocaleString()) string:

const src =  [1600934100,1602009600,1602747060,1603050158.390939,1603305573.992575],
             
      dates = src.map(n => 
        new Date(n*1e3)
          .toLocaleString(
            'en-US', 
            {month: 'short', year: 'numeric'}
          ))
     
console.log(dates)
.as-console-wrapper{min-height:100%;}

Leave a comment