단순히 현재 날짜를 가져 와서 1 년을 추가하고 렌더링해야하는 다음 구성 요소가 있습니다. 그러나 그것은 이상한 숫자를 렌더링
import React from 'react'
class Calendario extends React.Component {
constructor(props) {
super(props);
this.state = {
birth_date : new Date(),
years_to_live: 10,
death_date: ""
};
}
componentDidMount() {
var death_date = this.state.birth_date.setFullYear(this.state.birth_date.getFullYear() + 1) //this is should result in the current date +1
this.setState({ death_date : death_date});
}
render() {
return (
<div>
<p>Hello world!</p>
<p>{this.state.death_date}</p>
</div>
)
}
}
export default Calendario;
어떤 이유로 렌더링
1610227797517
대신에
Sat Jan 09 2021 22:21:12 GMT+0100 (Central European Standard Time)
무엇이 문제입니까?
.NET Date
용 자바 스크립트 객체를 사용하고 있기 때문에 날짜를 정수로 되돌리고 birth_date
있습니다.
필요한 작업 (포맷 등)을 수행하기 위해 날짜로 다시 구문 분석 할 수 있습니다.
var dt = new Date(1610227797517);
console.log(dt);
// output: Sat Jan 09 2021 16:29:57 GMT-0500 (Eastern Standard Time)
그러나 개체 자체에 형식을 사용할 수 있어야합니다.
console.log(this.state.death_date.toDateString());
// output: Sat Jan 09 2021
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다