Typescript Date Object

Learn via video courses
Topics Covered

Overview

The date object in typescript represents the date it provides the exact date in the application with the help of the date object., it is used to set or get the year, month and day, hour, minute, second, and millisecond by using the date object.

The date object passes the function which passes which follows the Greenwich Mean Time (GMT). By default, if any date class is created without specifications, it contains our computer's date.

Introduction

As we know that typescript date object is used to provide the exact date in the application. The Date() constructor in typescript returns an object that has a type of Date. The interface defines typings for all of the built-in methods on the Date object. We can access the user's date with the help of user-defined and custom functional requirements like Coordinated universal time(UTC), and Greenwich Mean Time(GMT).

By default in typescript if any date class is created without any specifications then it contains the date of our computer. We can get or set year, month and day, hour, minute, second, and millisecond fields of the object by Date methods.

So now let us see the syntax of how to create a date object in typescript :

Syntax :

The above line is the syntax of creating a date object in typescript.

Typescript Date Object Properties

There are only two date object properties in typescript. Let us read about each of them :

Constructor

This property in typescript specifies the function that creates an object's prototype.

Prototype

This property in typescript helps us to add properties and methods to a class.

Now let us read about typescript date object methods :

Typescript Date Object Methods

  1. Date() :
    This method in typescript is used to return the current date and time.
  2. getDate() :
    This method in typescript is used to return the day of the month for the specified date according to the region's local time.
  3. getHours() :
    This method in typescript is used to return hours on the specified date according to the region's local time.
  4. getFullYear() :
    This method in typescript is used to return the year of the specified date according to the region's local time.
  5. getMilliseconds() :
    This method in typescript is used to return milliseconds in the specified date according to the region's local time.
  6. getMinutes() :
    This method in typescript is used to return minutes on the specified date according to local time.
  7. getMonth() :
    This method in typescript is used to return the month on the specified date according to local time.
  8. getSeconds() :
    This method in typescript is used to return seconds in the specified date according to local time.
  9. getTimezoneOffset() :
    This method in typescript is used to return the time-zone offset in minutes for the current local region.
  10. getUTCDate() :
    This method in typescript is used to return the day of the month in the specified date according to the universal time zone (GMT)/(UTC).
  11. setUTCDate() :
    This method in typescript is used to sets the day(date) of the month for a specified date according to the universal time zone (GMT)/(UTC).
  12. setUTCFullYear() :
    This method in typescript is used to sets the full year in the specified date according to universal time.
  13. setUTCHours() :
    It is used to set the hours for a specified date according to universal time.
  14. setUTCMilliseconds() :
    This method in typescript is used to sets the milliseconds for a specified date according to universal time.
  15. setUTCMinutes() :
    This method in typescript is used to sets the minutes for a specified date according to universal time.
  16. toDateString() :
    This method in typescript is used to return the "date" portion of the date as a human-readable string.
  17. toLocaleDateString() :
    This method in typescript is used to return the "date" portion of the Date as a string, using the current locale's conventions.
  18. toLocaleFormat() :
    This method in typescript converts a date to a string, using a format string.
  19. toLocaleString() :
    This method in typescript converts a date to a string, using the current locale's conventions.
  20. toLocaleTimeString() :
    This method in typescript is used to return the "time" portion of the Date as a string, using the current locale's conventions.
  21. toSource() :
    This method in typescript is used to return a string representing the source for an equivalent Date object; you can use this value to create a new object.
  22. toString() :
    This method in typescript is used to return a string representing the specified Date object.
  23. toTimeString() :
    This method in typescript is used to return the "time" portion of the Date as a human-readable string.
  24. toUTCString() :
    This method in typescript converts a date to a string, using the universal time convention.
  25. getUTCHours() :
    This method in typescript is used to return hours on the specified date according to universal time. getTimezoneOffset() is used to return the time-zone offset in minutes for the current locale.
  26. getUTCDate() :
    It is used to return the day(date) of the month on the specified date according to universal time.
  27. getUTCDay() :
    It is used to return the day of the week on the specified date according to universal time.
  28. getUTCFullYear() :
    It is used to return the year on the specified date according to universal time.
  29. getUTCHours() :
    It is used to return hours on the specified date according to universal time.
  30. getUTCMilliseconds() :
    It is used to return milliseconds in the specified date according to universal time.
  31. getUTCMinutes() :
    It is used to return the minutes on the specified date according to universal time.
  32. getUTCDay() :
    It is used to return the day of the week on the specified date according to universal time.
  33. getUTCFullYear() :
    It is used to return the year on the specified date according to universal time.
  34. getUTCHours() :
    It is used to return hours on the specified date according to universal time.
  35. getUTCMilliseconds() :
    It is used to return milliseconds in the specified date according to universal time.
  36. getUTCMinutes() :
    It is used to return the minutes on the specified date according to universal time.
  37. toDateString() :
    It returns the “date” portion of the Date as a human-readable string.
  38. toGMTString() :
    It converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
  39. toLocaleDateString() :
    It returns the “date” portion of the Date as a string, using the current locale’s conventions.
  40. toLocaleFormat() :
    It converts a date to a string, using a format string.

Example :

As we have read about so many methods so let us see an example of how to implement them :

Output :

Explaination :
In the above code we have just used a few date methods which we had learned and simply implemented in this program. Run the above code in your editor for a better and clear explanation.

Creating TypeScript Date Object

There are four ways to create a new date object in typescript. Let us read about them in detail:

  1. new Date() :
    This method in typescript creates a new date object with the current date and time.

  2. new Date(datestring) :
    This method in typescript creates a new date object from a date string.

  3. new Date(milliseconds) :
    This method in typescript creates a new date object as zero time plus milliseconds.

  4. new Date ( year, month, date[, hour, minute, second, millisecond ]) :
    This method in typescript creates a new date object with a specified date and time. Now let us see an example :

    Output :

Conclusion

  • This is the default feature in typescript.
  • By using this property in typescript the user will access the applications in different regions and areas of the world.
  • The date function will calculate the date and time for performing the user inputs with the correct values.
  • The Date object represents the user's date and time in TypeScript.
  • By default, Typescript will use the browser's time zone and display a date as a whole text string.
  • We can set or get the year, month and day, hour, minute, second, and millisecond fields of the object by Date methods.
  • The date object in typescript encapsulates an integral number that represents milliseconds since midnight at the beginning of the year.