1
我必須設置每月15日倒計時。我能夠成功獲得倒計時到15日所需的差異值。倒計時值返回0
計算差異後,我計算天數,小時數,分鐘數,秒數。
一切除了天否則返回0
export default React.createClass({
tick: function() {
var currentDate = new Date();
var date_till_15 = new Date();
if (currentDate.getDate() < 15) {
var days_till_15 = 15 - currentDate.getDate();
date_till_15 = new Date(date_till_15.setDate(currentDate.getDate() + days_till_15));
} else if(currentDate.getDate() > 15){
date_till_15 = new Date(date_till_15.setMonth(currentDate.getMonth() + 1));
date_till_15 = new Date(date_till_15.setDate(15));
}
var difference = date_till_15 - currentDate;
var daysLeft = 0, hoursLeft = 0, minutesLeft = 0, secondsLeft = 0;
if (difference > 0) {
daysLeft = Math.floor(difference/(1000*60*60*24));
difference -= daysLeft * (1000*60*60*24);
hoursLeft = Math.floor(difference/(1000*60*60));
difference -= hoursLeft * (1000*60*60);
minutesLeft = Math.floor(difference/(1000*60));
difference -= minutesLeft * (1000*60);
secondsLeft = Math.floor(difference/1000);
this.setState({
days: daysLeft,
hours: hoursLeft,
minutes: minutesLeft,
seconds: secondsLeft
});
} else {
clearInterval(this.timeInterval);
this.setState({ expired: true });
}
},
componentDidMount: function(){
this.timeInterval = setInterval(this.tick.bind(this), 1000);
},
render() {
return (
<div>
{this.state &&
<div>
<div>{this.state.days}</div>
<div>{this.state.minutes}</div>
</div>
}
</div>
)
}
});
沒有到這個問題的解決方案恰好但你可以看看Moment.js,它比一個更容易使用本地Date()類。 http://momentjs.com/ – jered
絕對使用這個時刻 – John
你認爲你至少可以嘗試正確地設置你的代碼的格式嗎?閱讀目前的狀態非常困難。 –