JavaScript Objects (1)
JavaScript Objects (1)
You learned about primitive and structured data types in JavaScript. An object
is a non-primitive, structured data type in JavaScript. Objects are same as
variables in JavaScript, the only difference is that an object holds multiple
values in terms of properties and methods.
Above, p1 and p2 are the names of objects. Objects can be declared same
as variables using var or let keywords. The p1 object is created using the
object literal syntax (a short form of creating objects) with a property
named name. The p2 object is created by calling the Object() constructor
function with the new keyword. The p2.name = "Steve"; attach a
property name to p2 object with a string value "Steve".
Syntax:
var <object-name> = { key1: value1, key2: value2,...};
The following example demonstrates objects created using object literal syntax.
Note that the whole key-value pair must be declared. Declaring only a key
without a value is invalid, as shown below.
Example: hasOwnProperty()
var person = new Object();
The properties and methods will be available only to an object where they are
declared.
changeFirstName(person)
Nested Objects
An object can be a property of another object. It is called a nested object.
Points to Remember :
Object constructor:
var person = new Object();
person.firstName = "James";
person["lastName"] = "Bond";
person.age = 25;
person.getFullName = function () {
return this.firstName + ' ' + this.lastName;
};
Object properties and methods can be accessed using dot notation or [ ] bracket.
Use the Date() function to get the string representation of the current date and
time in JavaScript. Use the new keyword in JavaScript to get the Date object.
Date() Syntax
new Date()new Date(value)new Date(dateString)new Date(year,
monthIndex)new Date(year, monthIndex, day)new Date(year, monthIndex,
day, hours)new Date(year, monthIndex, day, hours, minutes)new Date(year,
monthIndex, day, hours, minutes, seconds)new Date(year, monthIndex, day,
hours, minutes, seconds, milliseconds)
Parameters:
No Parameters: A date object will be set to the current date & time if no
parameter is specified in the constructor.
value: An integer value representing the number of milliseconds since
January 1, 1970, 00:00:00 UTC.
dateString: A string value that will be parsed using Date.parse() method.
year: An integer value to represent a year of a date. Numbers from 0 to 99
map to the years 1900 to 1999. All others are actual years.
monthIndex: An integer value to represent a month of a date. It starts
with 0 for January till 11 for December
day: An integer value to represent day of the month.
hours: An integer value to represent the hour of a day between 0 to 23.
minutes: An integer value to represent the minute of a time segment.
seconds: An integer value to represent the second of a time segment.
milliseconds: An integer value to represent the millisecond of a time
segment. Specify numeric milliseconds in the constructor to get the date
and time elapsed from 1/1/1970.
The following example shows various formats of a date string that can be
specified in a Date() constructor.
You can use any valid separator in the date string to differentiate date
segments.
Specify seven numeric values to create a date object with the specified year,
month and optionally date, hours, minutes, seconds and milliseconds.
Example: Date
var date1 = new Date(2021, 2, 3); // Mon Feb 03 2021 var date2 = new
Date(2021, 2, 3, 10); // Mon Feb 03 2021 10:00 var date3 = new
Date(2021, 2, 3, 10, 30); // Mon Feb 03 2021 10:30 var date4 = new
Date(2021, 2, 3, 10, 30, 50); // Mon Feb 03 2021 10:30:50 var date5 =
new Date(2021, 2, 3, 10, 30, 50, 800); // Mon Feb 03 2021 10:30:50
Date Formats
JavaScript supports ISO 8601 date format by default - YYYY-MM-
DDTHH:mm:ss.sssZ
date.toLocaleDateString();'2/10/2015'
date.toISOString(); '2015-02-10T10:12:50.500Z'
date.toTimeString(); '15:42:50'
To get date string in formats other than the ones listed above, you need to
manually form the date string using different date object methods. The
following example converts a date string to DD-MM-YYYY format.
Note:
Use third party JavaScript Date library like datejs.com or momentjs.com to work with
Dates extensively in JavaScript.
Method Description
getDate() Returns numeric day (1 - 31) of the specified date.
getDay() Returns the day of the week (0 - 6) for the specified date.
getFullYear() Returns four digit year of the specified date.
getHours() Returns the hour (0 - 23) in the specified date.
getMilliseconds() Returns the milliseconds (0 - 999) in the specified date.
getMinutes() Returns the minutes (0 - 59) in the specified date.
getMonth() Returns the month (0 - 11) in the specified date.
getSeconds() Returns the seconds (0 - 59) in the specified date.
getTime() Returns the milliseconds as number since January 1, 1970, 00:00:00 UTC.
getTimezoneOffset() Returns the time zone offset in minutes for the current locale.
getUTCDate() Returns the day (1 - 31) of the month of the specified date as per UTC time zone.
getUTCDay() Returns the day (0 - 6) of the week of the specified date as per UTC timezone.
getUTCFullYear() Returns the four digits year of the specified date as per UTC time zone.
getUTCHours() Returns the hours (0 - 23) of the specified date as per UTC time zone.
getUTCMilliseconds() Returns the milliseconds (0 - 999) of the specified date as per UTC time zone.
getUTCMinutes() Returns the minutes (0 - 59) of the specified date as per UTC time zone.
getUTCMonth() Returns the month (0 - 11) of the specified date as per UTC time zone.
getUTCSeconds() Returns the seconds (0 - 59) of the specified date as per UTC time zone.
getYear() Returns the no of years of the specified date since 1990. This method is Deprecated
The following table lists all the set methods of Date object.
Method Description
setDate() Sets the day as number in the date object.
setFullYear() Sets the four digit full year as number in the date object. Optionally set month and date.
setHours() Sets the hours as number in the date object. Optionally set minutes, seconds and milliseconds.
setMilliseconds() Sets the milliseconds as number in the date object.
setMinutes() Sets the minutes as number in the date object. Optionally set seconds & milliseconds.
setMonth() Sets the month as number in the date object. Optionally set date.
setSeconds() Sets the seconds as number in the date object. Optionally set milliseconds.
setTime() Sets the time as number in the Date object since January 1, 1970, 00:00:00 UTC.
setUTCDate() Sets the day in the date object as per UTC time zone.
setUTCFullYear() Sets the full year in the date object as per UTC time zone
setUTCHours() Sets the hour in the date object as per UTC time zone
setUTCMilliseconds() Sets the milliseconds in the date object as per UTC time zone
setUTCMinutes() Sets the minutes in the date object as per UTC time zone
setUTCMonth() Sets the month in the date object as per UTC time zone
setUTCSeconds() Sets the seconds in the date object as per UTC time zone
setYear() Sets the year in the date object. This method is Deprecated
toDateString() Returns the date segment from the specified date, excludes time.
toGMTString() Returns a date string in GMT time zone.
toLocaleDateString() Returns the date segment of the specified date using the current locale.
toLocaleFormat() Returns a date string in default format.
toLocaleString() Returns a date string using a current locale format.
toLocaleTimeString() Returns the time segment of the specified Date as a string.
toString() Returns a string for the specified Date object.
toTimeString() Returns the time segment as a string from the specified date object.
toDateString() Returns the date segment from the specified date, excludes time.
toUTCString() Returns a string as per UTC time zone.
valueOf() Returns the primitive value of a Date object.