Introduction
JavaScript is a light-weight programming language used for the World Wide Web. The JavaScript Date
object is useful for checking the date and time a visitor arrives at your website.
This guide will walk you through using JavaScript to get the current date and time from a client.
Prerequisites
- Familiarity with JavaScript (including creating, saving, and running scripts)
Create the Date Object in JavaScript
The JavaScript Date
object helps when working with dates. To create a new object the with current date and time, add the Date
variable to your script:
<script>
var today = new Date();
</script>
Use the Get Method to Show the current Date in JavaScript
If you want to get the date in the YYYY-MM-DD format, edit the date_test.html
document, and add the following variable:
<script>
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
</script>
The second line consist of the following instructions:
today.getFullYear() – Uses the today variable to display the 4-digit year.
today.getMonth()+1 – Displays the numerical month – the +1 converts the month from digital (0-11) to normal.
today.getDate() – Displays the numerical day of the month.
If you prefer a different format, simply change the order of the commands.
Note: There is a dash between each command. This creates a dash between each segment of the date.
Display Hours, Minutes, and Seconds using JavaScript
To show the time in HH:MM:SS format, edit your script to look as follows:
<script>
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
</script>
today.getHours() – This uses the today variable to display the current hour. This uses a 24-hour clock.
today.getMinutes() – Displays the current minute reading.
today.getSeconds() – Displays the current seconds reading.
Note: There is a colon between each command. This places a colon between each numeric display so that it reads like a clock.
Show the Full Current Date and Time in JavaScript
Combine the two commands to show full date and time in the YYYY-MM-DD and HH:MM:SS formats. Edit your script as follows:
<script>
var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date+' '+time;
</script>
The final line combines the two other pieces of code. This instructs the system to display the full date next to the full time.
Test the Code: If you want to test or edit the code mentioned in this article, refer to our Learn How to Get Current Date & Time in JavaScript entry in CodePen.
Conclusion
You should now be able to write a simple piece of JavaScript to retrieve the current date and time. This can be useful for creating a timestamp, by linking this script with an action and writing it in to a log file.
Next you should also read
SysAdmin,DevOps and Development
How to Check Python Version in Linux, Mac, & Windows
October 1, 2019
Python is a popular programming language with different versions organized by release date. Certain…
Linux File Permission Tutorial: How to Check and Change Permissions
September 17, 2019
The file permission feature specifies how much power each user has over a given file or directory. To…
SysAdmin,DevOps and Development
22 Best Linux Text Editors for Programming & Coding
September 3, 2019
A text editor is an application that lets you type text. All Linux distributions come with built-in editors,…
SysAdmin,DevOps and Development
How To Install and Use PHP Composer on Ubuntu 18.04
August 29, 2019
Composer is an application that tracks dependencies for a project, allowing you to specify a set of libraries…
Author
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.