Home > OS >  How to get time with react-native-date-picker
How to get time with react-native-date-picker

Time:01-26

I want to get the time with react-native-date-picker. So I set the mode as time. But this gives in this format, Here time is not correct too.

2023-01-25T16:50:53.467Z

This is my code,

     <DatePicker
        mode="time"
        date={date}
        modal
        open={pickupTimeModal1}
        onConfirm={time => console.log(time)}
        onCancel={() => {
          setPickupTimeModal1(false);
        }}
      />

CodePudding user response:

I think this library gives you an entire date string.
You can convert the date to a format that you like:

new Date(Date.parse("2023-01-25T16:50:53.467Z")).toLocaleString();
// 1/25/2023, 8:50:53 PM
new Date(Date.parse("2023-01-25T16:50:53.467Z")).toLocaleDateString();
// 1/25/2023
new Date(Date.parse("2023-01-25T16:50:53.467Z")).toLocaleTimeString();
//8:50:53 PM

for better conversion, you can also use date-fns

CodePudding user response:

  let d = new Date();
  let hours = d.getHours();
  let minutes = d.getMinutes();
  let seconds = d.getSeconds() <= 9 ? `0${d.getSeconds()}` : d.getSeconds();

  console.log(`${hours}:${minutes}:${seconds}`);
  • Related