Skip to content

Instantly share code, notes, and snippets.

@BrianLitwin
Created February 27, 2019 21:24
Show Gist options
  • Save BrianLitwin/f9fd896e494f13cc88266a2a32febd0d to your computer and use it in GitHub Desktop.
Save BrianLitwin/f9fd896e494f13cc88266a2a32febd0d to your computer and use it in GitHub Desktop.
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