UI UX Design (36 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (51 Blogs)
  • The Complete WebDeveloper (34 Blogs)
  • ReactJS (17 Blogs)
  • JavaScript and JQuery Essentials Training (2 Blogs)
SEE MORE

How to Implement JavaScript Date Methods?

Last updated on Apr 05,2024 939 Views

25 / 31 Blog from JavaScript

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:

JavaScript Date

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.

Date - javascript date - edureka

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:

  • No Argument − The Date() constructor creates a Date object set to the current date and time.
  • milliseconds − When one numeric argument is passed, it is taken as the internal numeric representation of the date in milliseconds.
  • datestring − It is a string representation of a date, in the format accepted by the Date.parse() method.
  • year − This integer value represents the year. You should always specify the year in full.
  • month − It represents the month, beginning with 0 for January to 11 for December.
  • date − The date is used for representing the day of the month.
  • hour − This Integer value represents the hour of the day (24-hour scale).
  • minute − It represents the minute segment of a time reading.
  • second − This represents the second segment of a time reading.
  • millisecond − This represents the millisecond segment of a time reading.

Now that you know the different parameters used in JavaScript date, let’s move on to the different JavaScript date methods.

JavaScript Date Methods

There are different JavaScript Date Methods and here is a list along with their description:

PropertyDescription

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.

Example: Date 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.

Upcoming Batches For UI UX Design Certification Course
Course NameDateDetails
UI UX Design Certification Course

Class Starts on 5th October,2024

5th October

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to Implement JavaScript Date Methods?

edureka.co