Created
February 27, 2019 21:24
-
-
Save BrianLitwin/f9fd896e494f13cc88266a2a32febd0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function formatDate(date) { | |
function addZero(d) { | |
return d < 10 ? "0" + d : d | |
} | |
const day = addZero(date.getDay()) | |
const month = addZero(date.getMonth()) | |
var year = date.getFullYear() | |
return year + "-" + month + "-" + day | |
} | |
function initDate() { | |
const threeMonths = 1000 * 60 * 60 * 24 * 180 | |
const today = new Date().getTime() | |
const threeMonthsAgo = today - threeMonths | |
return formatDate(new Date(threeMonthsAgo)) | |
} | |
class Container extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = {date: initDate()} | |
} | |
render() { | |
const { date } = this.state | |
const setDate = (e) => { | |
this.setState({date: e.target.value}) | |
} | |
return(<SelectDate value={date} setDate={setDate}/>) | |
} | |
} | |
class SelectDate extends React.Component { | |
render() { | |
const {value, setDate} = this.props | |
return ( | |
<div> | |
<form > | |
<label>Max date: </label> | |
<input | |
type="date" | |
min="2016-01-01" | |
value={value} | |
onChange={setDate} | |
/> | |
</form> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment