Last active
May 4, 2020 03:26
-
-
Save anmolsukki/c3ca730650cd19fe1d01f338c6005246 to your computer and use it in GitHub Desktop.
[ ReactJs ] Date Time Event ( https://codesandbox.io/s/event-8x24w )
This file contains 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
import React from 'react'; | |
class Event extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
time: new Date() | |
} | |
} | |
// it runs after the component output has been rendered to the DOM | |
componentDidMount() { | |
this.clockID = setInterval( | |
() => this.tick(), | |
1000 | |
); | |
} | |
// it runs right before React unmounts and destroys our component | |
componentWillUnmount() { | |
clearInterval(this.clockID); | |
} | |
tick = () => { | |
this.setState({ | |
time: new Date() | |
}) | |
} | |
render() { | |
return ( | |
<h4>Current Date is: | |
<p>(DD/MM/YYYY format)</p> {this.state.time.toLocaleDateString('en-GB')}<hr/> | |
<p>(MM/DD/YYYY format)</p> {this.state.time.toLocaleDateString('en-US')}<hr/> | |
<p>(YYYY.M.D format)</p> {this.state.time.toLocaleDateString('ko-KR')}<hr/> | |
<p>Current Time is AM/PM format:</p> {this.state.time.toLocaleTimeString()}<hr/> | |
<p>Current Time is 24 Hours format:</p> {this.state.time.toTimeString()}<hr/></h4> | |
); | |
} | |
} | |
export default Event; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment