0

I am working on a project and want to set date and time one day and one hours ahead of current date and current time by default everytime whenever i come on that screen, by using a library (react-native-modal-datetime-picker) in react-native but unable to do that. Could you please let me know how to get this done? Thanks in Advance!!

import DateTimePickerModal from "react-native-modal-datetime-picker";

<DateTimePickerModal
            mode={mode}
            isVisible={isDateVisible}                //isDateVisible=true
            value={CurrentDateValue}
            display="default"
            onConfirm={handleDateConfirm}
            onCancel={onCancel}
        />
<DateTimePickerModal
            mode={mode}
            isVisible={isTimeVisible}                //isTimeVisible=true
            display="default"
            onConfirm={handleTimeConfirm}
            onCancel={onCancel}
        />
  • Add 1 day and then 1 hour in the current time to get the answer. See these: [How can I add 1 day to current date?](https://stackoverflow.com/q/9989382/2873538) [Adding hours to JavaScript Date object?](https://stackoverflow.com/q/1050720/2873538) – Ajeet Shah May 04 '21 at 19:08
  • Does this answer your question? [How to add 30 minutes to a JavaScript Date object?](https://stackoverflow.com/questions/1197928/how-to-add-30-minutes-to-a-javascript-date-object) – Ajeet Shah May 04 '21 at 19:08

2 Answers2

0

You can do something like this

  const [isDateVisible, setIsDateVisible] = React.useState(false);

  let initial = new Date(Date.now() + 3600 * 1000 * 25);
  // here 25 in last line means 1 day and 1 hour = 25 hours

  return (
    <View style={styles.container}>
      <DateTimePickerModal
        isVisible={isDateVisible} //isDateVisible=true
        date={initial}
        mode="datetime"
        onConfirm={(data) => {
          console.log(data);
          setIsDateVisible(false);
        }}
        onCancel={() => {
          setIsDateVisible(false);
        }}
      />
      <Button title="Pick Date Time" onPress={() => setIsDateVisible(true)} />
    </View>
  );

Working Example here

Run it on Android/iOS because DateTimePicker is not supported on: web

Kartikey
  • 4,516
  • 4
  • 15
  • 40
0
new Date(Date.now() + ( 3600 * 1000 * 25 ))

You can do like this here 3600 is the seconds and 25 is the hours meaning 1 day and 1 hour.

B. Raut
  • 129
  • 1
  • 2
  • 9