DEV Community

Cover image for JS: How to get current date and make your sites more up to date

Posted on • Updated on

JS: How to get current date and make your sites more up to date

Hello World! New episode of the series - A CSS/JS trick in 5 minutes.
Today we will study how date works in javascript (and how to get current date).

Javascript date objects allow us to work with date and time. We can retrieve information for it by creating a date and assign and assigning it to a variable:

let d = new Date(); // We usually call it d or date
Enter fullscreen mode Exit fullscreen mode

Date object provide us a lot of different methods, the most used are year, month, day, hours, minutes, seconds, and milliseconds. Remember that you always have to precise the entire year (1950 and not only 50), that we always start with 0 (so, for example, December is the eleventh, a minute is composed of 59 seconds...) and that day is in a 24 hours format.

You can then retrieve from date a lot of differents info:

d.getDate() Returns the day of the month (from 1-31)
d.getDay()  Returns the day of the week (from 0-6)
d.getFullYear() Returns the year
d.getHours()    Returns the hour (from 0-23)
d.getMilliseconds() Returns the milliseconds (from 0-999)
d.getMinutes()  Returns the minutes (from 0-59)
d.getMonth()    Returns the month (from 0-11)
d.getSeconds()  Returns the seconds (from 0-59)
Enter fullscreen mode Exit fullscreen mode

We can also set things:

d.setDate() Sets the day of the month of a date object
d.setFullYear() Sets the year of a date object
d.setHours()    Sets the hour of a date object
d.setMilliseconds() Sets the milliseconds of a date object
d.setMinutes()  Set the minutes of a date object
d.setMonth()    Sets the month of a date object
d.setSeconds()  Sets the seconds of a date object
d.setTime() Sets a date to a specified number of milliseconds after/before January 1, 1970
Enter fullscreen mode Exit fullscreen mode

Javascript dates have a lot of different methods, you can find them on w3school

We have 4 different methods to convert dates:

d = d.toString()
// Default method - Fri Feb 12 2021 21:06:40 GMT+0100
// Fri Feb 12 2021
// Fri, 12 Feb 2021 20:06:56 GMT
// 2021-02-12T20:09:18.183Z
Enter fullscreen mode Exit fullscreen mode
  • UTC stands for Coordinated Universal Time. It is within about 1 second of mean solar time at 0° longitude and is not adjusted for daylight saving time. It is effectively a successor to Greenwich Mean Time (GMT). More on Wikipedia

  • ISO (International Organization for Standardization)
    is an international standard covering the exchange of time-related data. The purpose of this standard is to provide an unambiguous and well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times. More on Wikipedia

We can also had a little that give us the date in a better form (dd/mm/yyyy):

let today = new Date();
let dd = String(today.getDate()).padStart(2, '0');
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0
let yyyy = today.getFullYear();

today = `${mm}/${dd}/${yyyy}`;
console.log(today); // 02/12/2021
Enter fullscreen mode Exit fullscreen mode

We create a new date and retrieve from it the day, month, and year. Then we reassign the three to the first variable in a better-looking way and print it in the console.

Hope this helped and thanks for reading!

Please smash that like button to make me understand that you want the series to continue :)

Check this article about how to write CSS like a pro!

Discussion (0)