How to convert the News API Date format to DD MM YY
Make money for being or staying online/internet.

You will get a $50 starting gift when you join using this code: Exode4LKrbujm1z and link:: GET THE OFFER NOW!!

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

Save up to 80% with this Domain & Shared Hosting package deal! 80% OFF - GET OFFER NOW

Related Post(s)

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

» Open Weather API - Source Code

» Mr.

» Mr.

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

collections_bookmark Category :: API
date_range Published :: 4 years ago At: 02:49 PM
event_note Detailed Date :: Apr 09th, 2020
person Writer :: Code
  • RECENT POSTS
1 day ago

Mr.

(sel


1 day ago

Mr.

(sel


1 day ago

Mr.

555


1 day ago

Mr.

555


1 day ago

Mr.

5550


1 day ago

Mr.

5550


1 day ago

Mr.

555


1 day ago

Mr.

555


Subscribe
  • ADVERTISEMENT

YOU MAY LIKE THESE POSTS

share