- Typescript Date Object
- Introduction
- Syntax :
- Typescript Date Object Properties
- Constructor
- Prototype
- Typescript Date Object Methods
- Example :
- Creating TypeScript Date Object
- Conclusion
- Typescript date get only date
- # Get the current date and time in TypeScript
- # Examples of commonly used Date object methods
- # Formatting the current date and time using TypeScript
- # Additional Resources
Typescript Date Object
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
- Date() :
This method in typescript is used to return the current date and time. - 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. - getHours() :
This method in typescript is used to return hours on the specified date according to the region’s local time. - getFullYear() :
This method in typescript is used to return the year of the specified date according to the region’s local time. - getMilliseconds() :
This method in typescript is used to return milliseconds in the specified date according to the region’s local time. - getMinutes() :
This method in typescript is used to return minutes on the specified date according to local time. - getMonth() :
This method in typescript is used to return the month on the specified date according to local time. - getSeconds() :
This method in typescript is used to return seconds in the specified date according to local time. - getTimezoneOffset() :
This method in typescript is used to return the time-zone offset in minutes for the current local region. - 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). - 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). - setUTCFullYear() :
This method in typescript is used to sets the full year in the specified date according to universal time. - setUTCHours() :
It is used to set the hours for a specified date according to universal time. - setUTCMilliseconds() :
This method in typescript is used to sets the milliseconds for a specified date according to universal time. - setUTCMinutes() :
This method in typescript is used to sets the minutes for a specified date according to universal time. - toDateString() :
This method in typescript is used to return the «date» portion of the date as a human-readable string. - toLocaleDateString() :
This method in typescript is used to return the «date» portion of the Date as a string, using the current locale’s conventions. - toLocaleFormat() :
This method in typescript converts a date to a string, using a format string. - toLocaleString() :
This method in typescript converts a date to a string, using the current locale’s conventions. - toLocaleTimeString() :
This method in typescript is used to return the «time» portion of the Date as a string, using the current locale’s conventions. - 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. - toString() :
This method in typescript is used to return a string representing the specified Date object. - toTimeString() :
This method in typescript is used to return the «time» portion of the Date as a human-readable string. - toUTCString() :
This method in typescript converts a date to a string, using the universal time convention. - 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. - getUTCDate() :
It is used to return the day(date) of the month on the specified date according to universal time. - getUTCDay() :
It is used to return the day of the week on the specified date according to universal time. - getUTCFullYear() :
It is used to return the year on the specified date according to universal time. - getUTCHours() :
It is used to return hours on the specified date according to universal time. - getUTCMilliseconds() :
It is used to return milliseconds in the specified date according to universal time. - getUTCMinutes() :
It is used to return the minutes on the specified date according to universal time. - getUTCDay() :
It is used to return the day of the week on the specified date according to universal time. - getUTCFullYear() :
It is used to return the year on the specified date according to universal time. - getUTCHours() :
It is used to return hours on the specified date according to universal time. - getUTCMilliseconds() :
It is used to return milliseconds in the specified date according to universal time. - getUTCMinutes() :
It is used to return the minutes on the specified date according to universal time. - toDateString() :
It returns the “date” portion of the Date as a human-readable string. - toGMTString() :
It converts a date to a string, using the Internet GMT conventions. Use toUTCString instead. - toLocaleDateString() :
It returns the “date” portion of the Date as a string, using the current locale’s conventions. - 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 :
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:
- new Date() :
This method in typescript creates a new date object with the current date and time. - new Date(datestring) :
This method in typescript creates a new date object from a date string. - new Date(milliseconds) :
This method in typescript creates a new date object as zero time plus milliseconds. - 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 :
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.
Typescript date get only date
Last updated: Jan 20, 2023
Reading time · 3 min
# Get the current date and time in TypeScript
Use the Date() constructor to get the current date and time in TypeScript, e.g. const now = new Date() .
When the Date() constructor is called without any arguments, it returns a Date object that represents the current date and time.
Copied!// 👇️ const now: Date const now = new Date(); console.log(now); // 👉️ 2023-01-20T12:01:02.900Z
We called the Date() constructor to get a Date object that represents the current date and time in TypeScript.
Date objects store a number that represents the number of milliseconds elapsed since the 1st of January 1970 UTC.
The type of the now variable is correctly inferred to be Date which enables us to use any of the built-in methods the Date object implements.
# Examples of commonly used Date object methods
Here are some examples of methods you might use on the Date object.
Copied!// 👇️ const now: Date const now = new Date(); console.log(now); // 👉️ 2023-01-20T12:01:41.216Z console.log(now.toLocaleDateString()); // 👉️ 1/20/2023 console.log(now.toLocaleString()); // 👉️ 1/20/2023, 2:01:41 PM console.log(now.toUTCString()); // 👉️ Fri, 20 Jan 2023 12:01:41 GMT console.log(now.toISOString()); // 👉️ 2023-01-20T12:01:41.216Z
# Formatting the current date and time using TypeScript
Here is an example that formats the date and time as YYYY-MM-DD hh:mm:ss , but can easily be tweaked to other formats, e.g. to MM/DD/YYYY or MM/DD/YYYY hh:mm:ss .
Copied!function padTo2Digits(num: number) return num.toString().padStart(2, '0'); > function formatDate(date: Date) return ( [ date.getFullYear(), padTo2Digits(date.getMonth() + 1), padTo2Digits(date.getDate()), ].join('-') + ' ' + [ padTo2Digits(date.getHours()), padTo2Digits(date.getMinutes()), padTo2Digits(date.getSeconds()), ].join(':') ); > // 👇️ YYYY-MM-DD hh:mm:ss // 👇️ "2023-01-20 14:03:10" (Current date and time) console.log(formatDate(new Date())); // 👇️️ 2025-05-04 05:24:07 (yyyy-mm-dd hh:mm:ss) console.log(formatDate(new Date('May 04, 2025 05:24:07')));
The function makes use of the following 6 methods on the Date object:
- Date.getFullYear method — returns a four-digit number representing the year that corresponds to a date.
- Date.getMonth — returns an integer between 0 (January) and 11 (December) and represents the month for a given date. Yes, unfortunately, the getMonth method is off by 1 .
- Date.getDate — returns an integer between 1 and 31 representing the day of the month for a specific date.
- Date.getHours — returns the hour for the specified date.
- Date.getMinutes — returns the minutes for a date.
- Date.getSeconds — returns the seconds of a specific date.
The getMonth method returns a zero-based month index from 0 to 11, meaning January is 0 and December is 11 .
The getMonth method is zero-based, so we added 1 to its return value.
We first created a padTo2Digits function that takes care of adding a leading zero if the month, day, hours, minutes or seconds only contain a single digit (are less than 10).
Copied!function padTo2Digits(num) return num.toString().padStart(2, '0'); > console.log(padTo2Digits(3)); // 👉️ '03' console.log(padTo2Digits(7)); // 👉️ '07' console.log(padTo2Digits(10)); // 👉️ '10'
We want to make sure that the result is always consistent and has 2 digits for the months, days, hours, minutes and seconds, so we used the padStart method.
The first parameter we passed to the padTo2Digits function is the total length of the string, so it will never pad a value if it already has 2 digits.
We placed the year, month and day in an array, so we can join them with a hyphen separator.
Copied!console.log(['2023', '02', '17'].join('-')); // 👉️ '2023-02-17' console.log(['2024', '07', '24'].join('-')); // 👉️ '2024-07-24'
This could have been any other separator, e.g. a forward slash / .
You could also easily reorder the date components if necessary (e.g. to MM/DD/YYYY ) by switching the places of the elements in the array.
This gets us the date formatted as YYYY-MM-DD .
The next step is to place the return values of the time-related methods in an array and join them with a colon separator.
Copied!console.log(['06', '30', '17'].join(':')); // 👉️ '06:30:17' console.log(['09', '11', '49'].join(':')); // 👉️ '09:11:49'
We used the same approach to format the time component as we did with the date components.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.