Basic Programming Tutorials

JavaScript Date

Welcome to the JavaScript tutorials, We have already covered JavaScript Boolean, JavaScript arrays and JavaScript string objects. Today we will discuss about the JavaScript date object. You will need a JavaScript date object, if you need to deal with the date and time in your coding. Again, like other objects, you can create JavaScript date object using its constructor.

JavaScript date object will let you to set the date, time, year and month etc. Even you can use the same object to get the date, time, year and month.

JavaScript date object – Syntax

You will use the constructor to create the JavaScript date object. You can you any of the syntax as per your need or liking.

var sharp = new Date ();

Here we have not passed any parameter or value. It means the constructor will create the JavaScript date object that will have the current date of the system.

var sharp = new Date (number);

The number will represent the time in milliseconds.

var sharp = new Date (dateString);

The dateString will represent the date. You cannot have any string format, make sure that string has to be in a format that date.parse () method can understand.

var sharp = new Date (year, month, day, hour , minutes , seconds , milliseconds);

year = You must have understood it with the name itself. Yes, it will represent the year.

month = Again, the name itself tells everything.

day = It will represent the days of the month.

Hour = It will represent the hours of the day.

Minutes = It will represent the minutes of the time.

Second = It will represent the seconds of the time.

Milliseconds = It will represent the milliseconds of the time.

Tips:

  • Only integer values to use in the respective parameters.
  • You can use optional things as per your need, like day, hour, minute, second, and millisecond.

JavaScript Date object – Properties

Now we will see the properties that you can use:

  • Constructor () – Like other JavaScript objects, It will return the function which made the object.
  • prototype () – It will let you add properties and methods to an object or objects.

JavaScript Date object – Methods

Here, we will list the methods that you can use:

  • Parse () – It will return the milliseconds after parsing the date string.
  • UTC () – It is a short for the Universal Coordinated time, and it is a time set by the world time standard. It will represent the milliseconds of the UTC time and date.

Setter methods

We called it setter methods, because you will set time, date, hours and etc.

  • setDate () – You can use this method to set the day of the month.
  • setMonth () – You can use this method to set the month of the date.
  • setTime () – You can use this method to set the time in milliseconds.
  • setFullYear () – You can set the year of the date object, in fact, you can use it to set the day and month as well.
  • setHours () – It will let you set the hours of the date object.

Getter methods:

We called it getter methods, because we will get the time, date, hours and etc.

  • getDate () – You can use this method to get the day of the month.
  • getMonth () – You can use this method to get the month.
  • getTime () – you can use this method to get the time in milliseconds.
  • getHours () – You can use this method to get the hours.
  • getFullYear () – It will return the year.

Note: The setYear () and getYear () has been deprecated.

We have not mentioned easy and every JavaScript date object method. In case you want to refer those methods, then refer to the JavaScript reference.

JavaScript Date Object – Examples

Exampe1 – Use of setter method.

In the below example, we have used setDate () method to show you the use of the setter method. Here we have set the date to 5, now let’s see what will be the output of the below code.

<html>

<body>

<script type="text/javascript">

var sharp = new Date();

sharp.setDate(15);

document.write(sharp);

</script>

</body>

</html>

The output will be:

Tue May 5 02:21:53 UTC-0400 2015

or

Tue May 05 2015 02:22:21 GMT-0400 (Atlantic Standard Time)

You will get any of the above results, why two different results? Well, you will get only one result, we have shown you the result of a window based editor and online editor.

Note: You can have any object name, as we are working for sharplesson, that is why using the relevant names.

Example 2 – Now we will see the use of getter methods.

The below example will show you the use of three getter methods, like getFullYear (), getMonth () and getDate (). Let’s see

<html>

<body>

<script type="text/javascript">

var sharp= new Date()

var sharpyear=sharp.getFullYear()

var sharpmonth=sharp.getMonth()+1

var sharptoday=sharp.getDate()

document.write("Today's date is: ")

document.write(sharptoday + "-"+ sharpmonth + "-" + sharpyear);

</script>

</body>

</html>

The output of the above code will be:

Today’s date is: 5-5-2015

It has just shown the result in the pattern we have set. You can set the result in another format as well.

Conclusion

We have just seen the JavaScript date object and its uses. We have shown you two examples to understand the methods in the proper way. Remember, you will understand the beauty of the JavaScript only if you will practice it.