Javascript Date object's get( ) methods
get( ) Method :
The get( ) method allows us to retrieve information about a given date,
such as the year, month, day, and more. The method takes a single argument,
which is the information we want to retrieve. For example, to get the year
of a date, we would call date.getFullYear().
Here are some of the most commonly used arguments for the get( ) method
:
- getFullYear(): Returns the year of the specified date, as a four-digit number.
- getMonth(): Returns the month of the specified date, as a number between 0 and 11.
- getDate(): Returns the day of the month of the specified date, as a number between 1 and 31.
- getDay(): Returns the day of the week of the specified date, as a number between 0 and 6 (0 representing Sunday, and 6 representing Saturday).
- getHours(): Returns the hour of the specified date, as a number between 0 and 23.
- getMinutes(): Returns the minute of the specified date, as a number between 0 and 59.
- getSeconds(): Returns the second of the specified date, as a number between 0 and 59.
- getMilliseconds(): Returns the millisecond of the specified date, as a number between 0 and 999.
- getTime(): Returns the number of milliseconds between the specified date and January 1, 1970 00:00:00 UTC.
Here's an example of how you might use the get method in your code:
let today = new Date();
document.write(today.getFullYear()+"<br>"); // Returns the year, e.g. 2023
document.write(today.getMonth()+"<br>"); // Returns the month, as a number between 0 and 11
document.write(today.getDate()+"<br>"); // Returns the day of the month, as a number between 1 and 31
document.write(today.getDay()+"<br>"); // Returns the day of the week, as a number between 0 and 6
document.write(today.getHours()+"<br>"); // Returns the Current Hours, between 0 - 23
document.write(today.getMinutes()+"<br>"); // Returns the current Minutes, between 0 - 59
document.write(today.getMilliseconds()+"<br>"); // Returns the Milliseconds between 0 - 999
document.write(today.getTime()+"<br>"); // Returns the Milliseconds since 1 January 1970
getFullYear( ) :
This method of the Date object is used to get the Year of any specific Date
or Current date.
It returns the Year of a specified date. Like 2023.
Syntax,
obj.getFullYear();
Example :
const Bday = new Date("2004-03-10");
document.write("Bday Year is : "+Bday.getFullYear());
Basically, it returns " Bday Year is : 2004 ".
getMonth( ) :
This method of the Date object is used to get the Month of any specified
Date or Current Date.
It will return the Month of a specified Date in the form of 0 - 11.
January is considered as 0 and the December is considered as 11.
Basically, it returns the Index Number of the Month. To get the Exact
Number of Months we need to add +1 after the method Calling.
Syntax,
obj.getMonth();
Example :
const Bday = new Date("2004-03-10");
document.write("Bday Month is : "+Bday.getMonth());
//Output : 2
var month = Bday.getMonth()+1;
document.write("Bday Month is : "+month);
By using the array of Months, we can able to get the Name of the Month.
const Bday = new Date("2004-03-10");
var month = ["January", "February", "March", "April", "May", "June",
"July", "August", "Saptember", "Octomber", "November", "December"];
document.write("Bday Month is : "+month[Bday.getMonth()]));
// Output : March
getDate( ) :
This method of the Date object is used to get the Day as a Number of any
specified Date or Current Date.
It returns a value between 1 - 31. It depends on how many days are present
in a Month.
Suppose the Month is February, then It will return the Date between 1 - 28
or 1 - 29.
Syntax :
obj.getMonth();
Example :
const date = new Date();
documnet.write("Todays Date is : "+date.getDate();
getDay( ) :
This method of the Date object is used to get the Day in the form of a
String of any specified Date or Current Date.
It returns the Value Between 0 -6 just like the days of a week. It
considers 0 as the First day of the week.
If we want to get the exact Number of a Day, we have to add +1 after method
calling.
Syntax :
obj.getDay();
Example :
const date = new Date();
document.write("Current Day is : "+date.getDay());
//After adding +1 at Method Calling
day = date.getDay() + 1;
document.write("Current Day is : "+day);
// Printing Name of Day
days = ["Sunday","Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday"];
document.write("Current Day is : "+days[date.getDay()]);
getHours( ) :
This method of the Date object is used to get the current Hour in the form of a String of Current Date.
It returns the Value Between 0 - 23. It considers 0 as 12 AM and 23 as 11 PM.
Syntax :
obj.getHours());
Example :
const today = new Date();
document.write("In 24 hours format ! ");
document.write("Current running Hour is : "+today.getHours()+"
");
getMinutes( ) :
This method of the Date object is used to get the current Minute in the form of a String of Current Date.
It returns the Value Between 0 - 59.
Syntax :
obj.getMinutes());
Example :
const date = Date();
document.write("Current Minute is : "+date.getMinutes());
getMilliseconds( ) :
This method of the Date object is used to get the current running Milliseconds in the form of a String of Current Date.
It returns the Value Between 0 - 999.
Syntax :
obj.getMilliseconds());
Example :
const today = new Date();
document.write("Current running Milliseconds is : "+today.getMilliseconds()+"<br><br>");
getTime( ) :
This method of the Date object is used to get the current running Milliseconds in the form of a String of Current Date since January 1 1970.
Syntax :
obj.getTime());
Example :
const today = new Date();
document.write("Current running Milliseconds since 1st January 1970 : "+today.getTime()+"
");
So this are the Methods of Date object to get the values of the Date object.
In Next Post we are going to know about Date objects set( ) methods.
I hope it will help you to learn more about Javascript !
Thank You !!!
0 Comments
Please do not add Any Spam link in Comments !