Skip to content

Variable length day and month formats #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ DatePicker component. Renders as a [React-Bootstrap InputGroup](https://react-bo
* **Callback Arguments:**
* `event` - Blur event.
* **Type:** `Event`
* `dateFormat` - Date format. Any combination of DD, MM, YYYY and separator.
* `dateFormat` - Date format. One of DD-MM-YYYY, MM-DD-YYYY or YYYY-MM-DD where - can be any separator. Alternatively DD and MM can be replaced with D and M for no zero prepending.
* **Optional**
* **Type:** `string`
* **Examples:** `"MM/DD/YYYY"`, `"YYYY/MM/DD"`, `"MM-DD-YYYY"`, or `"DD MM YYYY"`
* **Examples:** `"MM/DD/YYYY"`, `"YYYY/M/D"`, `"MM-DD-YYYY"`, or `"DD MM YYYY"`
* `clearButtonElement` - Character or component to use for the clear button.
* **Optional**
* **Type:** `string` or `ReactClass`
Expand Down
4 changes: 2 additions & 2 deletions example/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ const App = React.createClass({
</Col>
<Col sm={4}>
<FormGroup>
<ControlLabel>YYYY/MM/DD</ControlLabel>
<DatePicker dateFormat="YYYY/MM/DD" onChange={this.handleChange} value={this.state.date} />
<ControlLabel>YYYY/M/D</ControlLabel>
<DatePicker dateFormat="YYYY/M/D" onChange={this.handleChange} value={this.state.date} />
<HelpBlock>Help</HelpBlock>
</FormGroup>
</Col>
Expand Down
23 changes: 15 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,13 +381,19 @@ exports.default = _react2.default.createClass({
return (month > 9 ? month : '0' + month) + separator + (day > 9 ? day : '0' + day) + separator + date.getFullYear();
} else if (this.props.dateFormat.match(/DD.MM.YYYY/)) {
return (day > 9 ? day : '0' + day) + separator + (month > 9 ? month : '0' + month) + separator + date.getFullYear();
} else {
} else if (this.props.dateFormat.match(/YYYY.MM.DD/)) {
return date.getFullYear() + separator + (month > 9 ? month : '0' + month) + separator + (day > 9 ? day : '0' + day);
} else if (this.props.dateFormat.match(/M.D.YYYY/)) {
return month + separator + day + separator + date.getFullYear();
} else if (this.props.dateFormat.match(/D.M.YYYY/)) {
return day + separator + month + separator + date.getFullYear();
} else {
return date.getFullYear() + separator + month + separator + day;
}
},
handleBadInput: function handleBadInput(originalValue) {
var parts = originalValue.replace(new RegExp('[^0-9' + this.state.separator + ']'), '').split(this.state.separator);
if (this.props.dateFormat.match(/MM.DD.YYYY/) || this.props.dateFormat.match(/DD.MM.YYYY/)) {
if (this.props.dateFormat.match(/MM?.DD?.YYYY/) || this.props.dateFormat.match(/DD?.MM?.YYYY/)) {
if (parts[0] && parts[0].length > 2) {
parts[1] = parts[0].slice(2) + (parts[1] || '');
parts[0] = parts[0].slice(0, 2);
Expand Down Expand Up @@ -424,30 +430,31 @@ exports.default = _react2.default.createClass({
var month = void 0,
day = void 0,
year = void 0;
if (this.props.dateFormat.match(/MM.DD.YYYY/)) {
if (!inputValue.match(/[0-1][0-9].[0-3][0-9].[1-2][0-9][0-9][0-9]/)) {
if (this.props.dateFormat.match(/MM?.DD?.YYYY/)) {
if (!inputValue.match(/[0-1][0-9]?.[0-3][0-9]?.[1-2][0-9][0-9][0-9]/)) {
return this.handleBadInput(originalValue);
}

month = inputValue.slice(0, 2).replace(/[^0-9]/g, '');
day = inputValue.slice(3, 5).replace(/[^0-9]/g, '');
year = inputValue.slice(6, 10).replace(/[^0-9]/g, '');
} else if (this.props.dateFormat.match(/DD.MM.YYYY/)) {
if (!inputValue.match(/[0-3][0-9].[0-1][0-9].[1-2][0-9][0-9][0-9]/)) {
} else if (this.props.dateFormat.match(/DD?.MM?.YYYY/)) {
if (!inputValue.match(/[0-3][0-9]?.[0-1][0-9]?.[1-2][0-9][0-9][0-9]/)) {
return this.handleBadInput(originalValue);
}

day = inputValue.slice(0, 2).replace(/[^0-9]/g, '');
month = inputValue.slice(3, 5).replace(/[^0-9]/g, '');
year = inputValue.slice(6, 10).replace(/[^0-9]/g, '');
} else {
if (!inputValue.match(/[1-2][0-9][0-9][0-9].[0-1][0-9].[0-3][0-9]/)) {
if (!inputValue.match(/[1-2][0-9][0-9][0-9].[0-1][0-9]?.[0-3][0-9]?/)) {
return this.handleBadInput(originalValue);
}

year = inputValue.slice(0, 4).replace(/[^0-9]/g, '');
month = inputValue.slice(5, 7).replace(/[^0-9]/g, '');
day = inputValue.slice(8, 10).replace(/[^0-9]/g, '');
day = inputValue.slice(7).replace(/[^0-9]/g, '');
console.log(inputValue, year, month, day);
}

var monthInteger = parseInt(month, 10);
Expand Down
26 changes: 18 additions & 8 deletions src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,23 @@ export default React.createClass({
else if (this.props.dateFormat.match(/DD.MM.YYYY/)) {
return (day > 9 ? day : `0${day}`) + separator + (month > 9 ? month : `0${month}`) + separator + date.getFullYear();
}
else {
else if (this.props.dateFormat.match(/YYYY.MM.DD/)) {
return date.getFullYear() + separator + (month > 9 ? month : `0${month}`) + separator + (day > 9 ? day : `0${day}`);
}
else if (this.props.dateFormat.match(/M.D.YYYY/)) {
return month + separator + day + separator + date.getFullYear();
}
else if (this.props.dateFormat.match(/D.M.YYYY/)) {
return day + separator + month + separator + date.getFullYear();
}
else {
return date.getFullYear() + separator + month + separator + day;
}
},

handleBadInput(originalValue) {
const parts = originalValue.replace(new RegExp(`[^0-9${this.state.separator}]`), '').split(this.state.separator);
if (this.props.dateFormat.match(/MM.DD.YYYY/) || this.props.dateFormat.match(/DD.MM.YYYY/)) {
if (this.props.dateFormat.match(/MM?.DD?.YYYY/) || this.props.dateFormat.match(/DD?.MM?.YYYY/)) {
if (parts[0] && parts[0].length > 2) {
parts[1] = parts[0].slice(2) + (parts[1] || '');
parts[0] = parts[0].slice(0, 2);
Expand Down Expand Up @@ -404,30 +413,31 @@ export default React.createClass({
const inputValue = originalValue.replace(/(-|\/\/)/g, this.state.separator).slice(0,10);

let month, day, year;
if (this.props.dateFormat.match(/MM.DD.YYYY/)) {
if (!inputValue.match(/[0-1][0-9].[0-3][0-9].[1-2][0-9][0-9][0-9]/)) {
if (this.props.dateFormat.match(/MM?.DD?.YYYY/)) {
if (!inputValue.match(/[0-1][0-9]?.[0-3][0-9]?.[1-2][0-9][0-9][0-9]/)) {
return this.handleBadInput(originalValue);
}

month = inputValue.slice(0,2).replace(/[^0-9]/g, '');
day = inputValue.slice(3,5).replace(/[^0-9]/g, '');
year = inputValue.slice(6,10).replace(/[^0-9]/g, '');
} else if (this.props.dateFormat.match(/DD.MM.YYYY/)) {
if (!inputValue.match(/[0-3][0-9].[0-1][0-9].[1-2][0-9][0-9][0-9]/)) {
} else if (this.props.dateFormat.match(/DD?.MM?.YYYY/)) {
if (!inputValue.match(/[0-3][0-9]?.[0-1][0-9]?.[1-2][0-9][0-9][0-9]/)) {
return this.handleBadInput(originalValue);
}

day = inputValue.slice(0,2).replace(/[^0-9]/g, '');
month = inputValue.slice(3,5).replace(/[^0-9]/g, '');
year = inputValue.slice(6,10).replace(/[^0-9]/g, '');
} else {
if (!inputValue.match(/[1-2][0-9][0-9][0-9].[0-1][0-9].[0-3][0-9]/)) {
if (!inputValue.match(/[1-2][0-9][0-9][0-9].[0-1][0-9]?.[0-3][0-9]?/)) {
return this.handleBadInput(originalValue);
}

year = inputValue.slice(0,4).replace(/[^0-9]/g, '');
month = inputValue.slice(5,7).replace(/[^0-9]/g, '');
day = inputValue.slice(8,10).replace(/[^0-9]/g, '');
day = inputValue.slice(7).replace(/[^0-9]/g, '');
console.log(inputValue, year, month, day);
}

const monthInteger = parseInt(month, 10);
Expand Down