When it comes to the JavaScript date object, most tutorial websites/blogs usually just talk about how to initialize the date object to tell the actual date/ retrieve the current date from the device. They usually don’t go into how to how to perform actual calculations using the current date which is usually a problem I faced while I was building a project sometime ago.

In this project (e-commerce store) I created a sales table and I was trying to create a query to retrieve a list of sales made in the past 7 days. This was very difficult to handle until I came accross the getTime() method which enabled me to get the particular date in a series of numbers.

Let me show an example of this function.

var d= new Date;
var datenum= d.getTime();
document. write(datenum); 





This code above outputs 1621169388933 which is the date as at 16th of May 2021 1:50pm.

Confused right? Now I’m going to show you how the figures were gotten.

The getTime method gets the number of milliseconds starting from 1st of January 1970 at time 00:00:00. Note that 1 second equals 1000 milli seconds.

What this means is that if on the first of January 1970 at time 00:00:00, the count was 0, then on the 2nd January 1970 at time 00:00:00, the new time will be 86,400,000 since there are 86,400,000 milliseconds in one day. This also implies that on the 3rd January 1970 at time 00:00:00, the date count would be 172,000,000 which would be 86,400,000 multiplied by 3.

That’s how the date counts until it reached 16th of May 2021 at time 1:50pm which the count was now 1,621,169,388,933, Which is a very large number.

Now why do we have numerical dates in this format? Same thing I asked my self when I first saw it but during that project, I finally saw its use in dates manipulation which I’m going to share with you.

See how to create a JavaScript countdown for a website

JavaScript date add days.

Now if you can recall the project I talked about earlier, I wanted to create a script that retrieves a table of all the sales that occurred in the past 7 days. Now with the getTime method, this is possible through an algorithm I came up with after this.

Remember that one day contains 86,400,000 milliseconds. This implies that seven days will contain (86,400,000 * 7) milliseconds. Knowing this, if I can get the current date count of today using my getTime() method, I can easily subtract (86,400,000 *7) from the current count. If call this new count “Val” , i can easily set an SQL query to select all the sales in the database where the date count is greater than “Val”.

Note: It’s always advisabe to store each entry in the database with either their date counts and their normal string formats so SQL manipulations would be easier.

Let me give you an example of the query.

var d= new Date;

var datenum= d.getTime();

var sevendays= datenum- (86400000*7);

The “sevendays” variable now holds the date count for the past 7 days which would be sent to our backend script for SQL manipulations.

Php equivalent to the JavaScript getTime() method.

Don’t you think it would be cruel if only JavaScript was able to manipulate dates, well here’s a good news for php programmers. There’s an alternative to the getTime() method which is the “mktime()” function which retrieves the current date count starting from the 1st of January 1970. You can perform similar operations with this function and I prefer this because php is a backend programming language which after getting your values, you can put it into the SQL query direct unlike JavaScript which you’ll have to send it to the backend script using a http request before querying the database.

Php date add days.

The same manipulations with JavaScript is also done with php. The only difference is the name of the function which JavaScript is called getTime() while php is called mkTime().

Hope you enjoyed this article, please feel free to leave your feedback in the comments section. Also follow us on social media to stay up to date on our latest posts and algorithms.

You can also send us an email at hello@charmingdesk.com.ng for a quick and more private reply. Kudos on your web development journey.

Your favorite web developers!

Categories: JavaScript

vic okeke

I'm just a web developer hoping to help out junior developers in algorithms for their projects.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *