Write a javascript function to display the current day and time. In this JavaScript code example, Let’s see how to write a javascript program to display the current day and time before starting the program let’s understand how to display the current day and time.
Display the current day, date, and time in Java Script
If you want to get the date in the YYYY-MM-DD format then use this function.
- today.getFullYear() – uses to display the 4 digit year.
- today.getMonth()+1 – Displays numerical month – the +1 converts month from digital(0-11) to normal.
- today.getDate() – Displays numerical day of month.
- getHours() method is used to get the hour for a given date, according to local time. The value returned by getHours() is an integer between 0 and 23.
- getMinutes() method is used to get the minutes on the specified date according to local time. The value returned by getMinutes() is an integer between 0 and 59.
- getSeconds() method is used to get the seconds in the specified date according to local time. The value returned by getSeconds() is an integer between 0 and 59.
Algorithm to show the current day, date, and time
- Use getDay() function to display day.
- GetFullYear() to display full year
- GetDate() to display current date
Program to show the current day, date, and time
<!DOCTYPE HTML> <html> <head> <title>Display Current Date and Time </title> </head> <body> <h1> current date using JavaScript.</h1> <h2 id="displayDateTime"></h2> </body> <script type="text/javascript"> var today = new Date(); var day = today.getDay(); var daylist = ["Sunday","Monday","Tuesday","Wednesday ","Thursday","Friday","Saturday"]; var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate(); var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds(); var dateTime = date+' '+time; document.getElementById("displayDateTime").innerHTML = dateTime + ' <br> Day :- ' + daylist[day]; </script> </html>
Output
In this way, we learn how to write a javascript program to display the current day and time.
How to find the Current Month in JavaScript with different formats
JS provides Date objects to work with date & time, including days, months, years, hours, minutes, seconds, and milliseconds.
Use the Date() function to get the string representation of the current date and time in JavaScript. Use the new keyword in JavaScript to get the Date object. Create a date object by specifying different parameters in the Date() constructor function.
Date() Syntax
new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex)
new Date(year, monthIndex, day)…etc
JavaScript code to find current month in different formats
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Get month name in different formates</title> </head> <body> <script> //const monthNames = ["January", "February", "March", "April", "May", "June", //"July", "August", "September", "October", "November", "December" //]; const monthNames= ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const d = new Date(); document.write("The current month is " + monthNames[d.getMonth()]); </script> </body> </html>
Output
JavaScript code to find the Current Month in digits.
Use the Date() function to get the string representation of the current date and time in JavaScript. Use the new keyword in JavaScript to get the Date object. Create a date object by specifying different parameters in the Date() constructor function.
Display current month in digit in JavaScript
<!DOCTYPE html> <head> <title>javascript get current month number</title> </head> <body> <script type = "text/javascript"> var date = new Date(); var getMonthNumber = date.getMonth()+1; document.write( "javascript get month number is :- " + getMonthNumber ); </script> </body> </html>
Output
How to add Months, Year, and Days in the current date in JavaScript
To add months to a JavaScript Date, use the getMonth() method. JavaScript date getMonth() method sets the months for a specified date according to local time.
To add days to a JavaScript Date, use the getDate() method. JavaScript date getDate() method sets the months for a specified date according to local time.
To add months to a JavaScript Date, use the getYear() method. JavaScript date getYear() method sets the months for a specified date according to local time.
Java Script code to add days, months, years in current date
- use getMonth() method to add months
- use getDate() method to add days
- use getYear() method to add years
<!DOCTYPE html> <html> <head> <title>Add Days,Months,Year to a current Date </title> <body> <head> <script type="text/javascript"> var newDt = new Date(); document.writeln("Current Date :" + newDt + "<br/>" +"<br/>"); // add 6 days to the current date newDt.setDate(newDt.getDate() + 6); document.writeln("New Date :" + newDt + "<br/>" +"<br/>"); //add 2 months to current date var mydate = new Date(); mydate.setMonth(mydate.getMonth()+2); document.write("New Month:" + mydate +"</br>"+ "</br>"); var myYear = new Date(); myYear.setYear(myYear.getYear()+2); document.write("New Year:" + myYear +"</br>"); </script> </head> </body> </html>
Output
How to find Previous Months, Year and Days from the current date in JS.
To get the previous year in JavaScript, first, we need to access the current year by calling a getFullYear() method on the new Date() constructor and subtract it with -1.
The getFullYear() method returns the current year in four-digit(2021) format according to the user’s local time.
We use the setDate() method yesterday, passing as a parameter the current day minus one. Even if it’s day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous
month.
Java Script code to get the previous Month, Year, and Days
- Initialize today date
- Use function setDate() and getDate() function to get current date
- Initialize previous date() to get the previous date
- use const d
- Use setFullYear() and GetFullYear() to get previous year.
<html> <head> <meta charset="utf-8"> <title> Get previous date from current date</title> </head> <body> <script> <!-- var today =new Date(); var previous=new Date(); previous. setDate(today. getDate() - 1); document.write('Today Date: ' + today + "</br>"+ "</br>"); document.write('Previous Date: ' + previous + "</br>" +"</br>"); const d = new Date(); var toDateString=new Date(); d.setFullYear(d.getFullYear()-1); document.write( 'Previous year date :'+ d.toDateString()); </script> </body> </html>
Output
In this way, we learn how to get the previous date from the current date.
Find Date in MM/DD/YYYY format in JavaScript
The first few methods to use in this program is
-
- GetDate() method :
Syntax:Date.getDate()
Return value:
It returns a number, from 1 to 31, representing the day of the month. - getFullYear() method :
Syntax:Date.getFullYear()
Return value:
It returns a number, representing the year of the defined date - getMonth() method:
Syntax:Date.getMonth()
Return value:
It returns a number, from 0 to 11, representing the month - Use getMonth() method
- Use getDate() method
- Use getYear() method
- GetDate() method :
JavaScript code to get date in MM/DD/YYYY format
<!DOCTYPE HTML> <html> <head> <title> JavaScript | How to get current formatted date mm/dd/yyyy. </title> </head> <body style="text-align:center;" id="body"> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> get Date </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); el_up.innerHTML = today; var dd = today.getDate(); var mm = today.getMonth() + 1; var yyyy = today.getFullYear(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } var today = mm + '/' + dd + '/' + yyyy; function gfg_Run() { el_down.innerHTML = today; } </script> </body> </html>
Output
In this way, we learn how to find dates in MM/DD/YYYY format.
Find Date in YYYY/MM/DD format in JavaScript.
The first few methods to use in this program is
- GetDate() method :
Syntax:Date.getDate()
Return value:
It returns a number, from 1 to 31, representing the day of the month. - getFullYear() method :
Syntax:Date.getFullYear()
Return value:
It returns a number, representing the year of the defined date - getMonth() method:
Syntax:Date.getMonth()
Return value:
It returns a number, from 0 to 11, representing the month -
- Use getMonth() method
- Use getDate() method
- Use getYear() method
JavaScript code to get date in YYYY/MM/DD format
<!DOCTYPE HTML> <html> <head> <title> How to get current formatted date yyyy/mm/dd. </title> </head> <body style="text-align:center;" id="body"> <p id="GFG_UP" style="font-size: 15px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> get Date </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var today = new Date(); el_up.innerHTML = today; var dd = today.getDate(); var mm = today.getMonth() + 1; var yyyy = today.getFullYear(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } var today = yyyy + '/' + mm + '/' + dd; function gfg_Run() { el_down.innerHTML = today; } </script> </body> </html>
Output
In this way, we learn the date format YYYY/MM/DD in javascript.