Date Objects in JavaScript Properties and Methods with Examples

Date objects in javascript

Date Objects in JavaScript:

date objects is used to work with dates and times. it consists of properties (or constants) and methods. All these properties and methods can be called by directly date object.

The syntax for creating Date objects is:
var dd= new Date()

Where dd represents the variable of type Date. The Date object will automatically hold the current data and time its initial value.



Methods of Date objects:

The important and commonly used methods of Date object are as follows:

Date() Objects:

This method of date objects returns the current date and time of the computer system. The general syntax to use this method is:

Date();

The following statement displays the current date & time

 

JavaScript Objects

getDate() objects:

this method of date objects returns the date of the month. The value returned by this function is between 1 and 31. This method is always used in conjunction with a Date object. The general syntax to use this method is:

dateobject.getDate();

the follow statements display the day of current month:

 

JavaScript Objects

getDay():

this method of date objects returns the day of week. The value returned by this method is between 0 and 6. Value for Sunday is 0, Monday is 1 and so on. This method is always used in conjunction with a date object. The general syntax to use this method is:

dateObject.getDay();

getMonth():
this method of date objects returns the month of a year. The value returned by this function is between 0 and 11. The general syntax to use this method is:

dateobject.getMonth();

getFullYear():

this method of date objects returns a four-digit number that represents a year. The general syntax to use this method is:

dateobject.getFullYear();


getHours():

this method  of date objects returns the hour of a time. the general syntax to use this method is:

dateobject.getHours();

getMinutes():

this method of date objects returns the minutes of a time. the general syntax to use this method is:

dateObject.getMinutes();

getSeconds():

this method of date objects returns the seconds of a time. the general syntax to use this method is:

dateobject.getSeconds();

setDate():

this method of date objects is used to set the day of the month. The general syntax to use this method is:

dateobject.setDate(day);

where day represents a numeric value (from 1 to 31) of the month. The following statement set the day of month to 10

var d= new Date();

d.setDate(10);

setHours():

this method is used to set the hour of specified time. the general syntax t use this method is:

dataobject.setHours(hour);

where hour represents a numeric value for hour. Its value may be from 0 to 23. The following statement set the hour of current time to 12:

var d= new Date();

d.setHours(12);

setMinutes():

this method of date objects is used to set the minutes of specified time.

the general syntax to use this method is:

dateobject.setMinutes(min);

where min represents a numeric value for minutes. Its value may be from 0 to 59. The following statement set the minutes of current time to 30:

var d= new Date();

d.setMinutes(30);

SetSeconds():

This method is used to set the seconds of specified time.

The general syntax to use this method is:

Dateobject.setSeconds(sec);

Where sec represents a numeric value for seconds. Its value may be from 0 to 59. The following statement set the seconds of current time to 15:

Var d= new Date();

d.setSeconds(15);

setFullYear():

this setFullYear() method  is used to set the full year, indicating year, month and day. The general syntax to use this method is

dateobject.setFullYear(year,month,day);

setMonth():

this setMonth method  is used to set the month. The general syntax to use this method is:

dateobject.setMonth(month);

setYear():

the setYear() method is used to set the year. The general syntax to use this method is:

dateobject.setYear(year);

example write JavaScript code t display four images randomly in a table cell:

 

JavaScript Objects

JavaScript Objects

JavaScript Objects

JavaScript Objects



Example write JavaScript code to create a scrolling message in the browser’s status bar:

Related Article:

String Objects in JavaScript and SubString Objects Properties and Methods

Recommended For You

Fawad

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: