Lord have mercy on Lebanon. If you have anyone in Lebanon check on them. Pray for Lebanon
How to convert the News API Date format to DD MM YY

In this article, I will be answering and sharing with you a simple trick on how you can convert the news API date format into a user-friendly readable format.

As i was scrolling through my youtube comments, I came across this comment from a subscriber Vishal Soni stating; "Thank you for the source code its working fine. Please help me on changing the Date format into DD MM YY."

He left this comment on the How to display News with out searching - NEWS API tutorial and I hope this solution/trick helps him and you.

Tutorial
Tutorial Code

getTime() - The getTime() method returns the number of milliseconds between midnight of January 1, 1970, and the specified date.

getDate() - This method is used to get the day of the month of a given date according to local time. The value returned by the getDate() method is an integer between 1 and 31

getMonth() - The getMonth() method is used to get the month of a given date according to local time.

getFullYear() - This method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number. For dates between the years 1000 and 9999, getFullYear() returns a four-digit number, for example, 2020.

Why do we add 1 to the getMonth() method?

Well, The value returned by the getMonth method is an integer between 0 and 11, referring 0 to January, 1 to February, and so on.

And why the months start with zero in many programming languages is explained in here: Zero-based month numbering. Paraphrased: Using January == 0 was useful in ancient times, and now we are stuck with it.

So we add one(1) to the getMonth() method in order to go around that ancient way to numbering months.

copy

let newsApiDate = "2020-04-09T04:09:41Z"; // got from the Api

let timestamp = new Date(newsApiDate).getTime();
let Day = new Date(timestamp).getDate();
let Month = new Date(timestamp).getMonth() + 1;
let Year = new Date(timestamp).getFullYear();
let OurNewDateFormat = `${Day}/${Month}/${Year}`;

If in case you were following my previous tutorials about the news API, then you need to add this code in the for i loop in order to get access to the publishedAt date string as shown in the code snippet below.

copy

for(var i in latestNews){

let timestamp = new Date(latestNews[i].publishedAt).getTime();
let Day = new Date(timestamp).getDate();
let Month = new Date(timestamp).getMonth() + 1;
let Year = new Date(timestamp).getFullYear();
let OurNewDateFormat = `${Day}/${Month}/${Year}`;

}

//Make you output the OurNewDateFormat variable

CoronaVirus Alert

There’s currently no vaccine to prevent coronavirus disease (COVID-19). You can protect yourself and help prevent spreading the virus to others if you:

Do
  1. Wash your hands regularly for 20 seconds, with soap and water or alcohol-based hand rub.
  2. Cover your nose and mouth with a disposable tissue or flexed elbow when you cough or sneeze.
  3. Avoid close contact (1 meter or 3 feet) with people who are unwell.
  4. Stay home and self-isolate from others in the household if you feel unwell.
Don't

Touch your eyes, nose, or mouth if your hands are not clean

Related Post(s)

» How to create a search engine to search for news - News API | using Jquery

» How to display News with out searching - NEWS API(Source Code)

» Open Weather API - Source Code

» How to convert the News API Date format to DD MM YY

collections_bookmark Category :: API
date_range Published :: 6 months ago At: 02:49 PM
event_note Detailed Date :: Apr 09th, 2020
person Writer :: Code

YOU MAY LIKE THESE POSTS

share