Hello Friends 👋,
Welcome To Infinitbility! ❤️
To get the current date in react native has the Date()
object it will return every time the current date and time when we use it.
we have to just write new Date()
and it returns the date object where we get a year, month, date, hours, minutes, and seconds.
1let today = new Date();2console.log(today); // 2022-02-06T08:05:49.292Z
Now, we know we have the current date but we can’t show the same to also users.
To format, we have multiple packages like Day.js, and Moment.js.
But if wanna follow a custom formater follow the below code.
Below example will format date in yyyy-mm-dd
.
Let see how we can do in react native.
1import React, { useState, useEffect } from 'react';2import { Text, View, StyleSheet } from 'react-native';34export default function App() {5 const [date, setDate] = useState(null);67 useEffect(() => {8 let today = new Date();9 let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();10 setDate(date);11 }, []);1213 return (14 <View style={styles.container}>15 <Text style={styles.paragraph}>{'Current Date'} - {date}</Text>16 </View>17 );18}1920const styles = StyleSheet.create({21 container: {22 flex: 1,23 justifyContent: 'center',24 backgroundColor: '#ecf0f1',25 padding: 8,26 },27 paragraph: {28 margin: 24,29 fontSize: 18,30 fontWeight: 'bold',31 textAlign: 'center',32 },33});
Output

Thanks for reading…
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.