Reactjs 将时间转换为秒
代码:Reactjs 将时间转换为秒,reactjs,react-native,Reactjs,React Native,代码: var-moment=require('moment'); { this.setState({selctedStartTime:timeValue}) var timeStart=时刻(timeValue,“hh:mm A”).format('hh:mm A'); log('time:'+timeStart.getTime()); }} /> 我尝试将时间转换为秒来找出两个日期之间的差异,但是getTime()没有给出函数错误。我的代码有什么问题?因此,当您执行矩(timeValue,
var-moment=require('moment');
{
this.setState({selctedStartTime:timeValue})
var timeStart=时刻(timeValue,“hh:mm A”).format('hh:mm A');
log('time:'+timeStart.getTime());
}}
/>
我尝试将时间转换为秒来找出两个日期之间的差异,但是
getTime()
没有给出函数错误。我的代码有什么问题?因此,当您执行矩(timeValue,“hh:mm A”).format('hh:mm A')
时,它会将其转换为一个字符串,以hh:mm A
的格式表示日期
因此,字符串没有函数
getTime()
var newDate=moment(Date(timeStart)).format('HH:mm a');log('time:'+newDate.getTime());我将字符串转换为日期。得到相同的错误如果您的varnewDate
是一个字符串,您不会将其转换为日期对象,如果您需要秒,请尝试console.log(矩.duration(newDate.asSeconds())
或console.log(矩(newDate.asSeconds())
console.log(矩.duration(newDate.asSeconds())返回零秒
var moment = require('moment');
<DatePicker
date={this.state.selctedStartTime}
is24Hour={false}
showIcon={false}
mode="time"
format="hh:mm A"
confirmBtnText="Confirm"
cancelBtnText="Cancel"
customStyles={{
dateText: {
fontFamily: 'HelveticaNeue-Light',
fontSize:18,
fontWeight:'normal',
color:'rgb(0,178,192)',
marginLeft:20
},
dateInput: {
borderColor:'white'
}
}}
onDateChange={(timeValue) => {
this.setState({selctedStartTime: timeValue})
var timeStart = moment(timeValue, "hh:mm A").format('HH:mm A');
console.log('time:'+timeStart.getTime());
}}
/>