skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2.   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}`);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search