New Date (). getFullYear () in real

I'm trying to display the current year in the footer and trying to figure out how to get the current React method in the current year? Is there a way to use the new Date (). GetFullYear ()?

+4
source share
4 answers

You need to put Pure JavaScript inside {}. This works for me:

class HelloMessage extends React.Component {
  render() {
    return <div>{(new Date().getFullYear())}</div>;
  }
}

ReactDOM.render(<HelloMessage />, mountNode);

Compiled Version:

class HelloMessage extends React.Component {
  render() {
    return React.createElement(
      "div",
      null,
      new Date().getFullYear()
    );
  }
}

ReactDOM.render(React.createElement(HelloMessage, null), mountNode);
+7
source

You can simply add a variable const today = new Date();, and then add it <p> {today.getFullYear()} </p>to the current year if you want .

+2
source

, , , :

getYear() {
    return new Date().getFullYear();
}

, . , <span>, :

<span>
   © {this.getYear()} Your Name
</span>

:

© 2018 Your Name

With a well-named function outside the rendering method, you can quickly find out what the function does, and you have the option to change it in the future if, say, you want to change the copyright year, for example © 1996 - 2018, etc.

+1
source

You can use this code

new GregorianCalendar().getWeekYear()
Run code
0
source

Source: https://habr.com/ru/post/1664625/


All Articles