var sharp = new Date ();
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.
- 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.
Now we will see the properties that you can use:
- prototype () – It will let you add properties and methods to an object or objects.
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.
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.
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.
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.
The output will be:
Tue May 5 02:21:53 UTC-0400 2015
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
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.