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

You will get a $5 starting gift when you join using this 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

» Display Random Advice On Your Website or Web App Using Advice Slip JSON API

» YTS YIFY MOVIES API - PROJECT PREVIEW

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

» All Countries Drop Down List | HTML Select Country Name

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

How to force the browser to cache a page?

There are several ways to force a web browser to cache an HTML page:


3 weeks ago

How can i cache pages using php?

You can use the output buffering functions in PHP to cache pages. Output buffering allows you to store the output of a PHP script in a buffer, which you can then manipulate before sending it to the client.


3 weeks ago

PHP explained in a few lines

PHP is a popular programming language that is widely used for web development. It stands for "PHP: Hypertext Preprocessor" and is a server-side scripting language. This means that it is executed on the server, rather than in the user's web browser.


3 weeks ago

HTML explained in a few lines

HTML, or Hypertext Markup Language, is the standard markup language for creating web pages and web applications. It is used to structure and organize content on the web, and to create the basic structure and layout of a webpage.


3 weeks ago

CSS explained in a few lines

CSS, or Cascading Style Sheets, is a stylesheet language used for describing the look and formatting of a document written in HTML. CSS is used to control the presentation of multiple web pages at once, making it a crucial tool for web developers and designers.


1 year ago

A JavaScript library for formatting and manipulating numbers - Numeral.js

Check out this lightweight JavaScript library used for formatting and manipulating numbers.


1 year ago

All Countries Drop Down List | HTML Select Country Name

This simple country dropdown list is freely available for you to copy and use in your project forms.


1 year ago

HTML Entities Code Alphabet Discovery Using JavaScript

In this post I will show how writing just a few lines in JavaScript will allow you to render, browse and discover the alphabetical letters using a set of HTML entity codes.


Subscribe
  • ADVERTISEMENT

YOU MAY LIKE THESE POSTS

share