Categories
JavaScript Basics

Highlights of JavaScript — Dates and Rounding

Spread the love

To learn JavaScript, we must learn the basics.

In this article, we’ll look at the most basic parts of the JavaScript language.

Controlling the Length of Decimals

We can control the length of decimals by using the toFixed method.

For example, if we have:

let total = (8.8888).toFixed(2);

Then total is '8.89' . toFixed takes the number of digits to round to and returns string with the rounded number.

If the decimal ends with a 5, it usually rounded up.

But some browsers kay round down.

Get the Current Date and Time

To get the current date and time, we can create a new Date instance with new Date() .

Then we can convert the Date instance to a string by writing:

let now = new Date();
let dateString = now.toString();

To get the day of the week, we call the getDay method:

let now = new Date();
let day = now.getDay();

It returns a number from 0 to 6, with 0 being Sunday, and 6 being Saturday.

If we want to show a more human-readable date, we can write:

let dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
let now = new Date();
let day = now.getDay();
let dayName = dayNames[day];

We can use day as the index of dayNames to get the English version of the day of the week.

Extracting Parts of Date and Time

The Date instance comes with various methods to get various parts of a date and time.

getMonth returns the month of the date from 0 to 11, with 0 being January and 11 being December.

getDate gives us the number for the dat of the month.

getFullYear returns the 4 digit version of the year.

getHours gives us a number from 0 to 23, which corresponds to midnight and 11pm.

getMinutes returns the minutes of the hour from 0 to 59.

getSeconds returns the seconds of the minute from 0 to 59.

And getMilliseconds returns a number from 0 to 999.

getTime gives us the number of milliseconds that have elapsed since Jan 1, 1970 midnight UTC.

We can call them all from a date instance. For example, we can write:

let now = new Date();
let day = now.`getMilliseconds`();

Specify Date and Time

We can specify date and time with the Date constructor.

To return the current date and time, we write:

let today = new Date();

And we can calculate the number of days difference by subtracting the timestamp between 2 dates:

let msToday = new Date().getTime();
let msAnotherDay = new Date(2029, 1, 1).getTime()
let msDiff = msAnotherDay - msToday;

Then to get the number of days difference, we can write:

let daysDiff = msDiff / (1000 * 60 * 60 * 24);

If we want to return the round the days to a whole number, we round down with Math.floor :

daysDiff = Math.floor(daysDiff);

Conclusion

We can calculate the difference between days with the timestamps.

Also, we can round numbers to a given number of decimal places with toFixed .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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