I want to display a live year date in my footer in react, so it automatically changes every year, but it cant be changed whenever I change my pc clock, I looked in google for an npm or live clock API, but I couldn't find something that can give me the year date only, is there a way to do this?
Asked
Active
Viewed 978 times
3 Answers
2
Add this code to get the current year.
let currentDate = new Date();
let year = currentDate.getFullYear();
console.log(year);

Winter
- 331
- 1
- 8
-
using that way the year date gonna change every time I change my computer clock. – Abdulrahman May 05 '21 at 06:55
-
The date object is absolute time elapsed since Jan 01, 1970. I don't think it will change based on your computer settings. – Winter May 05 '21 at 07:12
0
If you want to deploy the website the best way is to use new Date()
Because if you do it with an npm that taking the value from an extern source it is just another request to somewhere for nothing every time that the component will rerender. the "Convention" is to do it with new Date().

Ethanolle
- 1,106
- 1
- 8
- 26
0
const Footer = () => {
// define and initialize the built in function Date from
// there you can call getFullYear built in functions too
const getCurrentYear = () => { return new Date().getFullYear();};
return(
// return output in frontend inside return( ... )
<p>{getCurrentYear()}</p>
)
} // end Footer

Wilfred
- 69
- 9