Table of contents
1.
Introduction
2.
How can we create a date object in Typescript?
3.
Date Object Properties in TypeScript
4.
Methods for Date Objects
4.1.
Example Using Methods
5.
Frequently Asked Questions
6.
Key Takeaway
Last Updated: Mar 27, 2024

Date Object in Typescript

Author Aditya Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In TypeScript, the Date object represents date and time capabilities. We can get or set the year, month, and day and the hour, minute, second, and millisecond.

When we create a date without passing any arguments to the constructor, it contains the date and time of the user's machine by default.

The Date object also has functions that deal with Coordinated Universal Time (UTC), also called Greenwich Mean Time (GMT). UTC time is used to calculate the World Time Standard.

How can we create a date object in Typescript?

A new date object can be created in four ways:

1. new Date(): The current date and time are used to build a new date object.

Example:

let date: Date = new Date();  
console.log("Date = " + date); //Date = Tue Feb 05 2019 12:05:22 GMT+0530 (IST)  

Output:

2. new Date(milliseconds): It produces a new date object with a time offset of zero + milliseconds.

Example:

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

Output:

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

Example:

let date: Date = new Date("2019-01-16");  
console.log("Date = " + date); //Date = Wed Jan 16 2019 05:30:00 GMT+0530 (IST)  

Output:

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

Example:

let date: Date = new Date(2018, 0O5, 0O5, 17, 23, 42, 11);  
console.log("Date = " + date); //Date = Tue Jun 05 2018 17:23:42 GMT+0530 (IST)  

Output:

Date Object Properties in TypeScript

There are two properties of Date object in Typescript which are given below:

Property

Description

constructor

It describes the function that builds the prototype of an object.

prototype

It allows you to customise an object by adding attributes and methods.

 

Methods for Date Objects

There are different kinds of methods in Date objects in Typescript which have different tasks and their return type will be a number. All the methods are given below:

Methods

Description

Date()

It gives you the current date and time.

getDate()

According to local time, it returns the day of the month for the supplied date.

getDay()

According to local time, it returns the day of the week for the supplied date.

getFullYear()

According to local time, it returns the year of the supplied day.

getHours()

According to local time, it returns the hour in the supplied date.

getMilliseconds()

According to local time, it returns the milliseconds in the supplied date.

getMinutes()

According to local time, it returns the minutes in the supplied date.

getMonth()

According to local time, it returns the month in the supplied date.

getSeconds()

According to local time, it returns the seconds in the supplied date.

getTime()

It returns the number of milliseconds since January 1, 1970, 00:00:00 UTC as the numeric value of the supplied date.

getTimezoneOffset()

It returns the current locale's time-zone offset in minutes.

getUTCDate()

According to universal time, it returns the day of the month in the supplied date.  

getUTCDay()

It returns the day of the week in universal time for the supplied date.

getUTCFullYear()

According to universal time, it returns the year at the supplied date.

getUTCHours()

According to universal time, it returns the hours in the supplied date.

getUTCMilliseconds()

According to universal time, it returns the milliseconds in the supplied date.

getUTCMinutes()

According to universal time, it returns the minutes in the supplied date.

getUTCMonth()

According to universal time, it returns the month in the supplied date.

getUTCSeconds()

According to universal time, it returns the seconds in the supplied date.

getYear()

According to local time, it returns the year on the supplied day. Instead, use getFullYear.

setDate()

It determines the day of the month for a given date using local time.

setFullYear()

According to local time, it sets the whole year for a specific date.

setHours()

According to local time, it sets the hours for a specific date.

setMilliseconds()

It sets the milliseconds for a specified date according to local time.

setMinutes()

According to local time, it sets the minutes on a specific day.

setMonth()

According to local time, it sets the month on a specific date.

setSeconds()

According to local time, it sets the seconds for a specific day.

setTime()

It sets the Date object to the time since January 1, 1970, 00:00:00 UTC, as expressed by a number of milliseconds.

setUTCDate()

According to universal time, it determines the day of the month for a given date.

setUTCFullYear()

According to universal time, it sets the complete year for a specific date.

setUTCHours()

According to universal time, it establishes the hour for a specific day.

setUTCMilliseconds()

According to universal time, it sets the milliseconds for a specific day.

setUTCMinutes()

According to universal time, it sets the minutes for a specific day.

setUTCMonth()

It determines the month for a given date using universal time.

setUTCSeconds()

It uses the universal time to set the seconds for a specific date.

setYear()

According to local time, it sets the year for a specific date. Instead, use setFullYear.

toDateString()

The "date" component of the Date is returned as a human-readable string.

toGMTString()

It uses the Internet GMT conventions to convert a date to a string. Instead, use toUTCString.

toLocaleDateString()

It uses the current locale's conventions to return the "date" component of the Date as a string.

toLocaleFormat()

It uses a format string to convert a date to a string.

toLocaleString()

It transforms a date into a string using the conventions of the current locale.

toLocaleTimeString()

It returns the "time" component of the Date as a string, using the norms of the current locale.

toSource()

It returns a string that represents the source of an equivalent Date object, which you may use to build a new one.

toString()

It returns a string that represents the Date object that was passed in.

toTimeString()

The "time" part of the Date is returned as a human-readable string.

toUTCString()

It uses the universal time convention to convert a date to a string.

valueOf()

It returns a Date object's primitive value.

Date.parse()

Returns the internal millisecond representation of a date and time based on a string representation.

Date.UTC()

The millisecond representation of the provided UTC date and time is returned.

 

Example Using Methods

let date: Date = new Date(2021, 6, 4, 17, 23, 42, 11);  
date.setDate(16);  
date.setMonth(13);  
date.setFullYear(2022);  
date.setHours(17);  
date.setMinutes(11);  
date.setSeconds(55);  
console.log("Year is = " + date.getFullYear());  
console.log("Date is = " + date.getDate());  
console.log("Month is = " + date.getMonth());  
console.log("Day is = " + date.getDay());  
console.log("Hours is = " + date.getHours());  
console.log("Minutes is = " + date.getMinutes());  
console.log("Seconds is = " + date.getSeconds());  

Output:

Frequently Asked Questions

1. What is a Date object in Typescript and why do we use it?

In TypeScript, the Date object represents date and time capabilities. We can get or set the year, month, and day, as well as the hour, minute, second, and millisecond.

When we create a date without passing any arguments to its constructor, by default, it contains the date and time of the user's machine by default.

 

2. What are the properties of Date Object in Typescript?

There are two properties of date object in Typescript which are given below:

  • constructor
  • prototype

 

3. How is it determined whether or not an object is a date object?

There are two methods to determine which are mentioned below:

  • Making use of the instanceof operator: The instanceof operator determines if a constructor's prototype property exists anywhere in an object's prototype chain. It is used in this scenario to determine whether the object is a Date object instance or not. A true value indicates that it corresponds to the specified object.

The !isNan() function can be used to check the validity of a date in a Date object. If the date is not invalid, it returns true.

Syntax:

object instanceof Date
  • Using the method Object.prototype.toString.call(): The Object.prototype.toString.call() method is used to return an object's internal class attribute in the format '[object Type]' in a string. During the creation of any object, this property is assigned internally. This attribute of the Date object can be tested by comparing it to the string '[object Date]'. A true value indicates that it corresponds to the specified object.

The !isNan() function can be used to check the validity of a date in a Date object. If the date is not invalid, it returns true.

Syntax:

Object.prototype.toString.call(object)

Key Takeaway

In this article, we have discussed date object in typescript. We have discussed its properties and its different types of methods. We have discussed what a date object actually is in typescript. A date is represented by a TypeScript Date object. Date methods can be used to access or set the object's year, month, and day fields, as well as the hour, minute, second, and millisecond fields. We discussed its methods like getDate(), getFullYear() and many more important methods.

You can take a look at our Typescript archives section and see many more interesting topics related to it.

Apart from that, you can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, and System Design.

Happy Learning!

Live masterclass