有没有办法使Material-UI DatePicker的值为String
而不是Date Object
?我只想要YYYY-MM-DD作为日期值。现在我正在做的是在将它发送到服务器之前进行编辑。Material-Ui DatePicker更改为字符串
0
A
回答
0
Date
Javascript中的对象具有将日期更改为字符串的原型函数。
var date = new Date();
var abc = date.toString();
console.log(abc); // Returns "Thu Feb 16 2017 12:01:19 GMT-0800 (PST)"
如果你指的是如何将日期更改为特定格式发送给服务器之前,你可以使用moment.js 是使用像 moment(dateObject, 'YYYY-MM-DD')
一个功能,您从DatePicker
提取dateObject
后。
这里是一个快速小提琴,显示时刻在行动格式: http://jsfiddle.net/fjz56wgg/
0
这是最好的材料UI日期选择器,我发现至今。它允许用户输入和选择一个日期,并显示一个字符串值(也可以用作输入值)。希望能帮助到你!
import React, { PropTypes } from 'react';
import TextField from 'material-ui/TextField';
import DatePicker from 'material-ui/DatePicker';
import Clear from 'material-ui/svg-icons/content/clear';
import DateIcon from 'material-ui/svg-icons/action/date-range';
import FontIcon from 'material-ui/FontIcon';
import format from 'date-fns/format';
import compose from 'recompose/compose';
import withHandlers from 'recompose/withHandlers';
import withState from 'recompose/withState';
import moment from 'moment';
const convertDateToYyyyMmDd = (date) => {
if (!date) return '';
return moment(date, 'DD/MM/YYYY').format('YYYY-MM-DD');
};
const enhance = compose(
withState('stringValue', 'changeStringValue', props => {
const { value } = props;
if (value) return format(props.value, 'DD/MM/YYYY');
return '';
}),
withState('valueOnFocus', 'changeValueOnFocus', ''),
withHandlers({
onChangeDatePicker: props => (event, val) => {
props.input.onChange(
format(val, 'YYYY-MM-DD')
);
props.changeStringValue(format(val, 'DD/MM/YYYY'));
},
onChange: props => (event, val) => props.changeStringValue(val),
onBlur: props => event => {
const { value } = event.target;
if (value != props.valueOnFocus) {
if (!value) {
props.input.onChange(null);
props.changeStringValue('');
} else {
const date = new Date(convertDateToYyyyMmDd(value));
props.input.onChange(date);
props.changeStringValue(format(date, 'DD/MM/YYYY'));
}
}
},
onFocus: props =>() => props.changeValueOnFocus(props.value),
clearDate: props =>() => {
props.input.onChange(null),
props.changeStringValue('');
}
}),
);
class MyDatePicker extends React.Component {
static propTypes = {
input: PropTypes.object,
mode: PropTypes.string,
floatingLabelText: PropTypes.string,
textFieldStyle: PropTypes.object,
tree: PropTypes.Object,
fieldName: PropTypes.string,
value: PropTypes.string | PropTypes.number,
stringValue: PropTypes.string | PropTypes.number,
errorText: PropTypes.string,
disabled: PropTypes.boolean,
onChangeDatePicker: PropTypes.func,
onChange: PropTypes.func,
clearDate: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
}
static defaultProps = {
value: null,
stringValue: '',
errorText: '',
disabled: false,
}
render() {
const {
errorText,
disabled,
onChangeDatePicker,
onChange,
onBlur,
onFocus,
clearDate,
input,
mode,
floatingLabelText,
textFieldStyle } = this.props;
const stringValue = this.props.stringValue ? this.props.stringValue : input.value ? format(input.value, 'DD/MM/YYYY') : '';
const valueDate = input.value ? new Date(input.value) : {};
return (
<div className="P-date-field">
<TextField
floatingLabelText={floatingLabelText}
type="text"
value={stringValue || ''}
errorText={errorText}
disabled={disabled}
fullWidth
onChange={onChange}
style={textFieldStyle}
ref="datePicker"
onBlur={onBlur}
onFocus={onFocus}
/>
<FontIcon
id="iconCalendar"
className="material-icons"
title="Open calendar"
onClick={!disabled ?() => this.datePicker.focus() : null}
>
<DateIcon />
</FontIcon>
<FontIcon
style={disabled ? {display: 'none'} : ''}
id="iconClear"
className="material-icons"
title="Clear date"
onClick={clearDate}
>
<Clear />
</FontIcon>
<div className="datePicker-hidden">
<DatePicker
id="dataPicker"
name={input.name}
floatingLabelText={''}
value={valueDate}
errorText={errorText}
disabled={disabled}
DateTimeFormat={window.Intl.DateTimeFormat}
locale="de-CH-1996"
formatDate={v => format(v, 'DD/MM/YYYY')}
mode={mode}
autoOk={true}
fullWidth
cancelLabel="Cancel"
onChange={onChangeDatePicker}
ref={c => (this.datePicker = c)}
/>
</div>
</div>
);
}
}
export default enhance(MyDatePicker);
相关问题
- 1. 更改WPF DatePicker的字符串格式
- 2. 字符串更改为空
- 3. 更改为字符串
- 4. 字符串键更改为数字键?
- 5. 更改字符串
- 6. UWP DatePicker自定义字符串为月
- 7. 将datepicker转换为字符串
- 8. 将字符串类型日期更改为日期字符串
- 9. 更改字符串编码WIN1250为utf8
- 10. C#将字符串更改为新值
- 11. 试图将字符串更改为altcase
- 12. 将字符串更改为int
- 13. 号码自动更改为字符串
- 14. 将PDO :: FECH_ASSOC更改为字符串
- 15. 将列表更改为字符串
- 16. 更改时间字符串为双
- 17. Chomp将字符串更改为1
- 18. 将1.000更改为1000字符串
- 19. 将sysout更改为字符串
- 20. 从ObjectId更改为字符串
- 21. 如何将字符串更改为Uri
- 22. 将unicode字符串更改为列表?
- 23. 如何将jquery datepicker更改为US datepicker
- 24. 更改字符串值
- 25. 更改连接字符串
- 26. NSDateFormatter更改字符串
- 27. 更改网址字符串
- 28. 更改连接字符串
- 29. 更改字符串值。 C#
- 30. javascript将字符串视为字符数组 - 如何更改该字符串?
这就是我实际上做的现在。我正在寻找一种方法,让它在提交之前在组件一侧进行更改。 – mateeyow
是否有一个原因,你需要的值是一个字符串,而不是组件本身的日期对象?如果你看看github上DatePicker组件的来源,那么组件中就有一些函数需要'value'属性作为日期对象,例如'formatDate'函数。一旦从组件中检索值,将该值更容易处理,而不是对该值进行任何修改作为字符串。 https://github.com/callemall/material-ui/blob/master/src/DatePicker/DatePicker.js –
'formatDate'函数只改变日期对象的外观。当你将它发送到服务器时它仍然是日期对象。数据库需要使用YYYY-MM-DD格式的日期数据类型 – mateeyow