JavaScript is the programming language used for the World Wide Web. Date Object in JavaScript Very useful for checking the date and time visitors come to your site. This tutorial will help you know how to use JavaScript to get the current Date Time from the client.
Create Date Object in JavaScript
To create a Date Object with the current date and time, add the variable today to your js file:
Use the Get Method to display the current Date in JavaScript
If you want to get dates in the format DD-MM-YYY, you need to add the following variable:
var date = today.getDate()+'-'+(today.getMonth()+first)+'-'+today.getFullYear(); |
- today.getFullYear () - Display the year with 4 digits.
- today.getMonth () + 1 - Display month.
- today.getDate () - Display the date.
If you prefer a different format, just change the order of the commands.
Display hours, minutes and seconds with JavaScript
To display the time in HH: MM: SS format, edit your file as follows:
var today = new Date(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); document.getElementById("hvn").innerHTML = time; |
- today.getHours () - Display hours.
- today.getMinutes () - Display minutes.
- today.getSeconds () - Display seconds.
Display the current date and time in JavaScript
Combine two code segments to display the full date and time in DD-MM-YYY and HH: MM: SS format. Edit your file as follows:
var today = new Date(); var date = today.getDate()+'-'+(today.getMonth()+1)+'-'+today.getFullYear(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); var dateTime = date+' '+time;
document.getElementById("hvn").innerHTML = dateTime; |
Conclude
Through this article we have shown you how to get the current date and time - current Date Time in javascript. If you have any contributions you can leave a comment below. You may also want to view our other shared posts here
0 Comments