What is the way to set the time 7am when the button returnToDefault
is pressed?
import DatePicker from 'react-native-date-picker';
import { View, Text, ScrollView, SafeAreaView, TouchableOpacity } from 'react-native';
import React, { useState } from 'react';
import moment from 'moment';
const Preferences = () => {
const [openHours, setOpenHours] = useState(false);
const [hours, setHours] = useState(new Date());
return (
<TouchableOpacity
onPress={() => setOpenHours(true)}
>
<Text>
{moment(hours).format('LTS')}
</Text>
</TouchableOpacity>
<DatePicker
modal
mode='time'
open={openHours}
date={hours}
onConfirm={orderDate => {
setOpenHours(false);
setHours(new Date(orderDate));
}}
onCancel={() => {
setOpenHours(false);
}}
/>
<TouchableOpacity
onPress={returnToDefault}
>
<Text>return to default</Text>
</TouchableOpacity>
);
};
CodePudding user response:
You can do something like this:
const returnToDefault=()=>{
setHours(moment()
.set('hours', 7)
.set('minutes', 0)
.set('seconds', 0)
.set('milliseconds', 0)
.toDate());
}
And, in TouchableOpacity,
{moment(hours).format('hh:mm')}
instead of
{moment(hours).format('LTS')}