-1

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!!!!

randelski
  • 1
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 21 '22 at 23:00

1 Answers1

0

you can directly use getDay function

new Date().getDay()

it will return the number

  • 0 = sunday
  • 1 = monday
  • 2 = tuesday
  • 3 = wednesday
  • 4 = thursday
  • 5 = friday
  • 6 = saturday
Bhavya Koshiya
  • 1,262
  • 2
  • 8
  • 22