- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Set Date Methods
The set date methods in JavaScript are −
Method | Description |
---|---|
setDate() | For setting the day as a number. |
setFullYear()() | For setting the year. |
setHours()() | For setting the hour. |
setMilliseconds() | For setting the milliseconds. |
setMinutes() | For setting the minutes. |
setMonth() | For setting the month. |
setSeconds() | For setting the seconds. |
setTime() | For setting the time. |
Following is the code for the set Date methods −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; color: red; } </style> </head> <body> <h1>JavaScript Set Date Methods</h1> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to set Date and display it</h3> <script> let sampleEle = document.querySelector(".sample"); let date = new Date(); document.querySelector(".Btn").addEventListener("click", () => { date.setHours(11); sampleEle.innerHTML = "setHours(11) = " + date + "<br>"; date.setMonth(1); sampleEle.innerHTML += "setMonth(1) = " + date + "<br>"; date.setMinutes(20); sampleEle.innerHTML += "setMinutes(20) = " + date + "<br>"; date.setFullYear(1990); sampleEle.innerHTML += "setMinutes(1990) = " + date + "<br>"; }); </script> </body> </html>
Output
On clicking the “CLICK HERE” button −
- Related Articles
- JavaScript - Get Date Methods
- How to set cookies expiry date in JavaScript?
- How to set JavaScript Cookie with no expiration date?
- How to set the day of a date in JavaScript?
- JavaScript Object Methods
- How to find duplicates in an array using set() and filter() methods in JavaScript?
- Share methods in JavaScript
- JavaScript Basic Array Methods
- JavaScript Date Formats
- Set Date value in Java HashMap?
- Static methods in JavaScript classes?
- Adding methods to Javascript Prototypes
- JavaScript date.@@toPrimitive() function
- JavaScript Date constructor Property
- How to set default date time as system date time in MySQL?

Advertisements