. Advertisement .
..3..
. Advertisement .
..4..
Today, we will learn how to type a Date object in Typescript. Firstly, date is an object that helps the programmer get the current time at the client machine, which will then process the functions that need this time parameter. Now let’s start with this article.
What is the response to the query about how to type a Date object in Typescript?
Method 1: Using Date() constructor
Date() constructor is the first way you can try to type a Date object in Typescript.
Syntax:
new Date(value)
new Date(dateString)
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
Parameters:
value is interpreted as the timestamp number. That many milliseconds have passed since January 1, 1970, 00:00:00 UTC.
dateString is a date in string format. According to the IETF complaint, it ought to be in RFC 2822 timestamp.
The third one takes the year, date, etc., to create a Date. Except for dates, all values start at 0.
Return Value
function Date() { [native code] }
Example:
const d = new Date();
let text = d.constructor;
Example:
let firstDate = new Date('December 20, 2020 10:30:00')
let secondDate = new Date('2020-12-20T10:30:00')
let thirdDate = new Date('2020-12-20')
let fourthDate = new Date('20th December, 2020')
console.log(firstDate)
console.log(secondDate)
console.log(thirdDate)
console.log(fourthDate)
As we can see, for the firstDate, secondDate, and thirdDate strings, it will correctly parse them; however, for the fourthDate string, it will not.
Result:
2020-12-20T05:00:00.000Z
2020-12-20T05:00:00.000Z
2020-12-20T00:00:00.000Z
Invalid Date
Method 2: Using Date parse() method
The next method you might consider applying is the Date parse() method.
Syntax:
Date.parse(dateString)
Parameters
dateString: The date is stored as a string in this parameter.
Return value
a value generated by parsing the provided text representation of a date that indicates the milliseconds that have passed since January 1, 1970, 00:00:00 UTC and the date. NaN is returned if the argument doesn’t represent a legitimate date.
Description
The parse() method provides the milliseconds since January 1, 1970, 00:00:00 UTC when a date string is supplied, such as “2011-10-10T14:48:00”.
For instance, this function can be used in conjunction with the setTime() method and the Date object to set date values depending on string values.
Method 3: Using momentjs
Moment.js is a great library to work with date objects on a browser field and Nodejs environment. To use Moment.js with TypeScript, we can make advantage of the types that are provided by moment.
In this case, we write:
import * as moment from "moment";
class DateClass {
constructor() {}
public getDate(): moment.Moment {
return moment();
}
}
To import the entire moment module with
import * as moment from "moment";
After that, we change the return type of the getDate method to moment. Moment – type for objects returned by moment. Therefore, returning moment() matches the moment. Moment and the TypeScript compiler will accept this code.
Then you may refer to the following example:
let parsedDate = moment(dateStr,"YYYY-MM-DD");
let outputDate = parsedDate.format("DD-MM-YYYY");
The value of parsedDate was retained after the date was extracted from the string dateStr. We are defining the dateStr’s format in this sentence. The format method converts the parsed date to the desired output format.Method 4 new Date().
Conclusion
Thank you for reading. ITtutoria hopes that after the solutions given in the article, the question of how to type a Date object in Typescript will no longer be your concern. Don’t forget to leave your thoughts in the comments section. Good luck!
Read more:
Leave a comment