简体   繁体   中英

How to get time with react-native-date-picker

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);
        }}
      />

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

  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}`);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM