I saw a code as reference where it showed the output of the currentTime, currentDay, the date, month, year in react native using extend class Component
export default class NowShowing extends Component {
constructor() {
super();
this.state = { currentTime: null, currentDay: null };
this.daysArray = [
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday',
];
}
UNSAFE_componentWillMount() {
this.getCurrentTime();
}
getCurrentTime = () => {
let hour = new Date().getHours();
let minutes = new Date().getMinutes();
let seconds = new Date().getSeconds();
let am_pm = 'PM';
if (minutes < 10) {
minutes = '0' + minutes;
}
if (seconds < 10) {
seconds = '0' + seconds;
}
if (hour > 12) {
hour = hour - 12;
}
if (hour == 0) {
hour = 12;
}
if (new Date().getHours() < 12) {
am_pm = 'AM';
}
this.setState({
currentTime: hour + ':' + minutes + '' + am_pm,
});
this.daysArray.map((item, key) => {
if (key == new Date().getDay()) {
this.setState({ currentDay: item.toUpperCase() });
}
});
};
componentWillUnmount() {
clearInterval(this.timer);
}
componentDidMount() {
this.timer = setInterval(() => {
this.getCurrentTime();
}, 1000);
}
render(
return(
<View >
<Text >{this.state.currentTime}</Text>
<Text >{this.state.currentDay} | {new Date().getMonth()}/
{new Date().getDate()}/{new Date().getFullYear()}</Text>
</View>
);
)
it is possible convert it into a function or is there another way of doing this code showing the output of the current day of the week??
Thanks so much in advanced!!!!