Date Object | timeline | set & get date from date object

 The Date object represents a date and time functionality in TypeScript. 

The Date object provides the functions which deal with Coordinated Universal Time (UTC) time or Greenwich Mean Time (GMT). The World Time Standard is based on UTC time.

Date object allows us to get or set the year, month and day, hour, minute, second, and millisecond.

If we create a date without any argument passed to its constructor, by default, it contains the date and time of the user's computer.


Creating Date Object

There are four ways to create a new date object:

1. new Date(): It creates a new date object with the current date and time.

2. new Date(milliseconds): It creates a new date object as zero time plus milliseconds.

3. new Date(datestring): It creates a new date object from a date string.

4. new Date ( year, month, date[, hour, minute, second, millisecond ]): It creates a new date object with a specified date and time.


1. new Date(): It creates a new date object with the current date and time.

Example

  1. let date: Date = new Date();  
  2. console.log("Date = " + date); //Date = Sun Sep 13 2020 14:48:22 GMT+0530 (India Standard Time) 

2. new Date(milliseconds): It creates a new date object as zero time plus milliseconds.

Example

  1. let date: Date = new Date(500000000000);  
  2. console.log("Date = " + date); //dateTue Nov 05 1985 06:23:20 GMT+0530 (IST)

new Date(datestring): It creates a new date object from a date string.

Example

  1. let date: Date = new Date("2018-12-09");  
  2. console.log("Date = " + date); //dateSun Dec 09 2018 05:30:00 GMT+0530 (India Standard Time)  

4. new Date ( year, month, date[, hour, minute, second, millisecond ]): It creates a new date object with a specified date and time.

Example

  1. let date: Date = new Date(2020, 012+1, 009, 17, 23, 42, 11); 
  2. console.log("Date = " + date); // date = Wed Dec 09 2020 17:23:42 GMT+0530 (India Standard Time)

Date Object Properties

PropertyDescription
constructorIt specifies the function that creates an object's prototype.
prototypeIt allows to add properties and methods to an object.

Date Object Methods

SNMethodDescription
1.Date()It is used to returns the current date and time.
2.getDate()It is used to returns day of the month for the specified date according to local time.
3.getDate()It is used to returns day of the week for the specified date according to local time.
4.getFullYear()It is used to returns year of the specified date according to local time.
5.getHours()It is used to returns hours in the specified date according to local time.
6.getMilliseconds()It is used to returns milliseconds in the specified date according to local time.
7.getMinutes()It is used to returns minutes in the specified date according to local time.
8.getMonth()It is used to returns month in the specified date according to local time.
9.getSeconds()It is used to returns seconds in the specified date according to local time.
10.getTime()It is used to returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
11.getTimezoneOffset()It is used to returns the time-zone offset in minutes for the current locale.
12.getUTCDate()It is used to returns the day(date) of the month in the specified date according to universal time.
13.getUTCDay()It is used to returns day of the week in the specified date according to universal time.
14.getUTCFullYear()It is used to returns the year in the specified date according to universal time.
15.getUTCHours()It is used to returns hours in the specified date according to universal time.
16.getUTCMilliseconds()It is used to returns milliseconds in the specified date according to universal time.
17.getUTCMinutes()It is used to returns the minutes in the specified date according to universal time.
18.getUTCMonth()It is used to returns the month in the specified date according to universal time.
19.getUTCSeconds()It is used to returns the seconds in the specified date according to universal time.
20.setDate()It is used to sets the day of the month for a specified date according to local time.
21.setFullYear()It is used to sets the full year for a specified date according to local time.
22.setHours()It is used to sets the hours for a specified date according to local time.
23.setMilliseconds()It is used to sets the milliseconds for a specified date according to local time.
24.setMinutes()It is used to sets the minutes for a specified date according to local time.
25.setMonth()It is used to sets the month for a specified date according to local time.
26.setSeconds()It is used to sets the seconds for a specified date according to local time.
27.setTime()It is used to sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
28.setUTCDate()It is used to sets the day(date) of the month for a specified date according to universal time.
29.setUTCFullYear()It is used to sets the full year in the specified date according to universal time.
30.setUTCHours()It is used to sets the hours for a specified date according to universal time.
31.setUTCMilliseconds()It is used to sets the milliseconds for a specified date according to universal time.
32.setUTCMinutes()It is used to sets the minutes for a specified date according to universal time.
33.setUTCMonth()It is used to sets the month for a specified date according to universal time.
34.setUTCSeconds()It is used to sets the seconds for a specified date according to universal time.
35.toDateString()It is used to returns the "date" portion of the date as a human-readable string.
36.toLocaleDateString()It is used to returns the "date" portion of the Date as a string, using the current locale's conventions.
37.toLocaleFormat()It converts a date to a string, using a format string.
38.toLocaleString()It converts a date to a string, using the current locale's conventions.
39.toLocaleTimeString()It is used to returns the "time" portion of the Date as a string, using the current locale's conventions.
40.toSource()It is used to returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
41.toString()It is used to returns a string representing the specified Date object.
42.toTimeString()It is used to returns the "time" portion of the Date as a human-readable string.
43.toUTCString()It converts a date to a string, using the universal time convention.
44.valueOf()It is used to returns the primitive value of a Date object.

Example

  1. let date: Date = new Date(2017, 4, 4, 17, 23, 42, 11);  
  2. // Thu May 04 2017 17:23:42 GMT+0530 (India Standard Time)
  3. date.getFullYear()
  4. date.getMonth()
  5. date.getDate()
  6. date.getHours()
  7. date.getMinutes()
  8. date.getSeconds()
  9. date.getMilliseconds()

  10. date.setDate(13);  
  11. date.setMonth(13);  
  12. date.setFullYear(2013);  
  13. date.setHours(13);  
  14. date.setMinutes(13);  
  15. date.setSeconds(13);  
  16.     //    Wed Feb 13 2013 13:13:13 GMT+0530 (India Standard Time)
  17. console.log("Year = " + date.getFullYear());  // Year = 2013
  18. console.log("Date = " + date.getDate());   // Date = 13
  19. console.log("Month = " + date.getMonth());  // Month = 1 (Feb)
  20. console.log("Day = " + date.getDay());  // Day = 3
  21. console.log("Hours = " + date.getHours());  // Hours = 13
  22. console.log("Minutes = " + date.getMinutes());  // Minutes = 13
  23. console.log("Seconds = " + date.getSeconds());  // Seconds = 13

Comments

Popular posts from this blog

Become a Expert + Secret of Success -- ii

Change Detection

without writing media queries How to make responsive component