I'm currently using this format to obtain time from the API data (
)
const d = new Date(eventData.ends_on);
const endTime = `${d.getHours()}:${d.getMinutes()${d.getSeconds()}`
if i would like to add AM/PM like this
, How would the format look like in the above code. Pls do help me out.
Below is my code, I would like to implement it in this code:
return (
<ScrollView style={[GlobalStyle.CustomScrollView]}>
<HeaderBar3/>
<Text style={[GlobalStyle.EventTitle]}> Event Details</Text>
<View style={[GlobalStyle.EventDetailView]}>
<Image style={[GlobalStyle.EventDetailImage]} source={{uri: eventData.main_image}} resizeMode="contain"/>
<Text style={[GlobalStyle.EventSubtitle]}>Date:</Text>
<Text style={[GlobalStyle.EventDate]}>{date}</Text>
<Text style={[GlobalStyle.EventTime]}>{endTime}</Text>
</View>
</ScrollView>
);
One option is to use the toLocaleString method, so you can provide the options you need. To use the default locale just pass an empty array to the function.
const d = new Date()
const time = d.toLocaleString([], {
hour: 'numeric',
minute: 'numeric',
hour12: true
})
console.log(time)
To use it with a predefined date, just change the first line:
const d = new Date(item.ends_on)