React JS Training Course Online
- 21k Enrolled Learners
- Weekend
- Live Class
We look for libraries like Date-fns and Moment when we need to work with date and time in JavaScript. But you can just use the built-in date object to access the current date or time while writing code in JavaScript. In this article, we will discuss how to implement the JavaScript date object in the following sequence:
The JavaScript date object is used to obtain year, month and day. You can display a timer on the webpage with the help of JavaScript date object. Also, you can use different Date constructors to create the date object. This provides methods to get and set day, month, year, hour, minute and seconds.
There are different ways to create a Date object using Date() constructor.
Syntax:
new Date( ) new Date(milliseconds) new Date(datestring) new Date(year,month,date[,hour,minute,second,millisecond ])
Parameters:
Now that you know the different parameters used in JavaScript date, let’s move on to the different JavaScript date methods.
There are different JavaScript Date Methods and here is a list along with their description:
Property | Description |
Date() | It returns today’s date and time. |
getDate() | It returns the day of the month for the specified date according to local time. |
getDay() | This gives the day of the week for the specified date according to local time. |
getFullYear() | This returns the year of the specified date according to local time. |
getHours() | It returns the hour in the specified date according to local time. |
getMilliseconds() | Shows the milliseconds in the specified date according to local time. |
getMinutes() | This returns the minutes in the specified date according to local time. |
getMonth() | It returns the month in the specified date according to local time. |
getSeconds() | Returns the seconds in the specified date according to local time. |
getTime() | This returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. |
getTimezoneOffset() | This returns the time-zone offset in minutes for the current locale. |
getYear() | It returns the year in the specified date according to local time. |
setDate() | Used to set the day of the month for a specified date according to local time. |
setFullYear() | It sets the full year for a specified date according to local time. |
setHours() | It sets the hours for a specified date according to local time. |
setMilliseconds() | This sets the milliseconds for a specified date according to local time. |
setMinutes() | Sets the minutes for a specified date according to local time. |
setMonth() | It sets the month for a specified date according to local time. |
setSeconds() | It sets the seconds for a specified date according to local time. |
setTime() | This sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC. |
These were some of the most commonly used date methods. Now let’s take an example and see how the date objects are used in JavaScript.
Let’s take an example to print date/month/year:
<script> var date=new Date(); var day=date.getDate(); var month=date.getMonth()+1; var year=date.getFullYear(); document.write("<br>Date is: "+day+"/"+month+"/"+year); </script>
Output:
Date is: 17/9/2019
Now, let’s take another example to print current time of system:
Current Time: <span id="txt"></span> <script> var today=new Date(); var h=today.getHours(); var m=today.getMinutes(); var s=today.getSeconds(); document.getElementById('txt').innerHTML=h+":"+m+":"+s; </script>
Output:
Current Time: 17:56:8
With this, we have come to the end of our article. I hope you understood how JavaScript Date objects are used.
Ready to launch your UI/UX career? Enroll in our UI Design Course today!
Check out our Full Stack Web Developer Masters Program which comes with instructor-led live training and real-life project experience. This training makes you proficient in skills to work with back-end and front-end web technologies. It includes training on Web Development, jQuery, Angular, NodeJS, ExpressJS, and MongoDB.
Check out the Angular Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Angular is a JavaScript framework that is used to create scalable, enterprise, and performance client-side web applications. With Angular framework adoption being high, performance management of the application is community-driven indirectly driving better job opportunities.
If you want to get trained in React and wish to develop interesting UI’s on your own, then check out the React JS Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of “JavaScript Date” blog and we will get back to you.
Course Name | Date | Details |
---|---|---|
UI UX Design Certification Course | Class Starts on 22nd February,2025 22nd February SAT&SUN (Weekend Batch) | View Details |
edureka.co