import React from 'react';
import { Text, Button } from 'react-native';
import RNDateTimePicker from '@react-native-community/datetimepicker';
const Timer = ({}) => {
const [picker, setPicker] = React.useState({time: null, date: null});
const [mode, setMode] = React.useState(null);
const setTimer = (event, time) => {
if (mode == 'date') {
if (time) setPicker(e => ({...e, date: time}));
setMode('time');
} else if (time && picker.date ) {
setPicker(e => ({...e, time: time}));
// do something here
}
}
const humanReadable = () => {/* return human readable date time format (2022/05:31 13:59:00) */}
return (
<>
<Text>{ humanReadable() }</Text>
<Button value="Get Date & Time" onPress={() => setMode("date")} />
{
mode && (
<RNDateTimePicker
onChange={setTimer}
mode={mode}
display={mode=='date' ? "calendar" : "clock"}
themeVariant="light"
value={picker[mode] ?? new Date()}
/>
)
}
</>
)
}
Beacuse i m working on android. So i have no option for set mode "datetime". That is why i have added here this. My question is how to get human readable datetime
CodePudding user response:
Maybe look at this lib https://date-fns.org
Here's how I use it :
//IMPORT FIRST
import { format } from 'date-fns';
import { fr } from 'date-fns/locale';
//THEN IN YOUR JSX
<Text>
{format(dateValue, 'dd MMMM yyyy', { locale: fr })}
</Text>
CodePudding user response:
You can use moment.js moment.js
Its an amazing module to format date. Please have a look at it.