D
D
Denis2019-05-07 18:27:40
JavaScript
Denis, 2019-05-07 18:27:40

Fetch, request with git hub api, how to properly format received dates?

https://codepen.io/fristyr/pen/arzQWL
I display information about my repositories by clicking, there is a date of the
item.updated_at
last update, but not in the format in which I would like, can it be formatted somehow? For example, to display "March 4, 2019".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-05-07
@fristyr

https://momentjs.com/

moment.locale('ru');

document.querySelector('#load').addEventListener('click', function load() {
  fetch('https://api.github.com/users/fristyr/repos')
    .then(r => r.json())
    .then(data => {
      const formatIn = 'YYYY-MM-DDTHH:mm:ss';
      const formatOut = 'DD MMMM YYYY';

      document.querySelector('.repo-wrapp').insertAdjacentHTML('afterbegin', data.map(item => `
        <article class="repo"> 
          <a href="${item.html_url}" class="repo__name">${item.name}</a>
          <br>
          <span class="repo__technology">${item.language}</span> 
          <span class="repo__update">${moment(item.updated_at, formatIn).format(formatOut)}</span>
        </article>`
      ).join(' '));
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question