Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strapi date format using javascript

I recieve this date format from strapi 2021-09-01T15:21:39.862Z how can i transform it to a DD/MM/YYYY format ?

<div class="post-date-article">{{ formatMyDate(article.published_at) }}</div>

formatMyDate(value){
     if (value) {
       return value;
      }
  }
like image 666
Yassine TAKEDDINE Avatar asked Feb 27 '26 05:02

Yassine TAKEDDINE


1 Answers

Strapi are providing a date in ISO 8601 format, which is easy to parse using the JavaScript date object.

I'd suggest using Date.toLocaleDateString() to format the input date as DD/MM/YYYY. Using a locale of 'en-GB' will format the supplied date in this way.

You can pass other locales such as 'de-DE' to format the date in another way if you wish.

function formatMyDate(value, locale = 'en-GB') {
    return new Date(value).toLocaleDateString(locale);
}

const timestamp = '2021-09-01T15:21:39.862Z';
console.log('Timestamp:', timestamp);
console.log('Formatted date:', formatMyDate(timestamp));
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 157
Terry Lennox Avatar answered Mar 01 '26 17:03

Terry Lennox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!