reactjs - 如何连接日期和时间
问题描述
我将日期和时间分开,当我保存时,我希望将日期和时间一起保存。所以请提出一种方法,我如何将日期和时间连接在一起。我正在使用的代码如下所示。
import React, { Component, Fragment } from 'react';
import moment from 'moment';
import PropTypes from 'prop-types';
import Datetime from 'react-datetime';
import { Button, Card, CardBody, CardFooter, CardHeader, Col, Row, Form, Modal, ModalHeader, ModalBody, ModalFooter, FormGroup, Input } from 'reactstrap';
import 'react-datepicker/dist/react-datepicker.css';
import '../../../node_modules/react-datetime/css/react-datetime.css';
class NewCreation extends Component {
constructor(props) {
super(props);
this.state = {
startDate: moment(),
startTime: moment(),
endDate: moment(),
endTime: moment(),
};
this.submitClick = this.submitClick1.bind(this)
}
handleStartDate(date) {//selectedDate:moment()
this.setState({startDate: moment(date).format('LL')});
this.forceUpdate()
}
handleStartTime(time) {
this.setState({ startTime: moment(time).format('HH:mm:ss') });
this.forceUpdate()
}
handleEndDate(date) {
this.setState({ endDate: moment(date).format('LL') });
this.forceUpdate()
}
handleEndTime(time) {
this.setState({ endTime: moment(time).format('HH:mm:ss') });
this.forceUpdate()
}
// SAVING Details
submitClick(e){
//on saving i wish to save the date and time together like ---> "2018-10-30 00:00:00.000"
}
render() {
return (
<Row>
<Col md="12">
<Form action="" method="post" encType="multipart/form-data" className="form-horizontal">
<FormGroup row>
<Col md="3"><span>Start Date</span><Datetime dateFormat={true} timeFormat={false} onChange={this.handleStartDate.bind(this)}/></Col>
<Col md="3"><span>Start Time</span><Datetime dateFormat={false} timeFormat={true} onChange={this.handleStartTime.bind(this)}/></Col>
<Col md="3"><span>End Date</span><Datetime dateFormat={true} timeFormat={false} onChange={this.handleEndDate.bind(this)}/></Col>
<Col md="3"><span>End Time</span><Datetime dateFormat={false} timeFormat={true} onChange={this.handleEndTime.bind(this)}/></Col>
</FormGroup>
</Form>
<div className="card-header-actions">
<Button type="button" size="sm" color="primarydark" onClick={this.submitClick}><i className="fa fa-dot-circle-o"></i> <span>Save And Continue</span></Button>
</div>
</Col>
</Row>
);
}
}
export default NewCreation;
我正在使用 react-datetime 来选择日期和时间。
解决方案
我看到你有你的 startTime 和 endTime 与 moment(),这个怎么样
submitClick(e){
this.setState({
fullTime: moment().format(); // 2018-10-12T15:27:30+08:00
})
}
推荐阅读
- reactjs - 可重用反应组件中的道具类
- python - 查找具有某个字符的 N 个非连续实例的单词?
- python - /cart 处的 NoReverseMatch
- c - 我的模拟没有运行或循环
- java - 设计咨询:从多个类调用方法
- python - 如何匹配和合并熊猫数据框与列表?
- javascript - 我在 JavaScript 的上下文中不理解这个关键字
- ubuntu - Docker 容器创建由 root 拥有的目录,我需要它们由 1000:1000 拥有
- django - Django 客户模板标签在我的生产设置中不起作用
- java - 我的教授在 setter 中跳过了一个变量,他写的代码是不是有错误?