How to get the previous year in TypeScript:
In this post, we will learn how to get the previous year of a given date in TypeScript. We can use the same methods defined for the JavaScript date object in TypeScript. We can use the getFullYear method to get the current year and if we subtract 1, it will return the previous year.
There was another method called getYear to get the current year for a date object. This method is deprecated now and we have to use getFullYear instead of getYear. Even though a few browsers might still support this, it is not recommended to use getYear as it doesn’t return the full year.
Definition of getFullYear:
The getFullYear method is defined as like below:
date.getFullYear()
Where date is the given date. It returns a number that is the current year according to local time. It returns a four-digit number.
Print the previous year in TypeScript:
We can use getFullYear to get the full year for a date object and subtract 1 from this value to get the previous year. Let’s take a look at the below example program to understand how it works:
let givenDate = new Date(2022, 1, 12);
console.log('Given date: ', givenDate);
let prevYear = givenDate.getFullYear() - 1;
console.log('Previous year: ',prevYear)
Here, givenDate is the given date object. It is 12th January, 2022. We called getFullYear method on the date object to get the full year and subtract 1 from this value to get the previous year. The previous year’s value is stored in the variable prevYear and the last line is printing this value.
If you run this program, it will print the below output:
Given date: 2022-02-11T18:30:00.000Z
Previous year: 2021
As you can see here, it is printing the previous year, i.e. 2021.
Change the year of the date to the previous year:
Sometimes, we might need to change the year of the given date object to the previous year. We can do that. The date object is a mutable object and we have to use a method called setFullYear to change the year of the date object. We can change the date to the previous year.
Definition of setFullYear:
This method is defined as like below:
setFullYear(year)
setFullYear(year, month)
setFullYear(year, month, date)
Here,
- year is an integer value that specifies the year value.
- month is an integer value to specify the month value. It is an optional value. We can pass a value between 0 to 11 to represent a month between January to December.
- date is also an optional value. It is an integer to represent the date of the year. We can pass a value between 1 to 31. We can pass this value along with month’s value. Otherwise, it will not work.
Return value of setFullYear:
The setFullYear method returns the number of milliseconds between 1 January 1970 00:00:00 UTC and the new date.
Example of setFullYear:
Let’s take a look at the below example:
let givenDate = new Date(2022, 1, 12);
console.log('Given date: ', givenDate);
givenDate.setFullYear(givenDate.getFullYear() - 1);
console.log('Date after change: ',givenDate)
It will change the date object givenDate to decrement the year by 1. It modifies the original date object givenDate. If you run this program, it will print the below output:
Given date: 2022-02-11T18:30:00.000Z
Date after change: 2021-02-11T18:30:00.000Z
You might also like:
- How to convert any to string in typescript
- TypeScript string search method
- How to create an array of objects in TypeScript
- TypeScript program to add two numbers
- 4 ways in TypeScript to reverse a string
- How to compile and run a TypeScript program
- How to get the current date in TypeScript
- How to add one day to a date object in TypeScript