JavaScript and JQuery Essentials Trainin... (56 Blogs) Become a Certified Professional
AWS Global Infrastructure

Front End Web Development

Topics Covered
  • AngularJS (29 Blogs)
  • jQuery UI Development (2 Blogs)
  • The Complete WebDeveloper (38 Blogs)
  • ReactJS (7 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement JavaScript Date Methods?

Published on Sep 18,2019 53 Views
25 / 29 Blog from JavaScript

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

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.

Got a question for us? Please mention it in the comments section of “JavaScript Date” blog and we will get back to you.

Comments
0 Comments

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.