1

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?

3 Answers3

2

Add this code to get the current year.

let currentDate = new Date();
let year = currentDate.getFullYear();
console.log(year);
Winter
  • 331
  • 1
  • 8
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