Navigate back to the homepage

How to get current date in react native?

Infinitbility
React Native
February 14th, 2022 · 1 min read
How to get current date in react native?

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';
3
4export default function App() {
5 const [date, setDate] = useState(null);
6
7 useEffect(() => {
8 let today = new Date();
9 let date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
10 setDate(date);
11 }, []);
12
13 return (
14 <View style={styles.container}>
15 <Text style={styles.paragraph}>{'Current Date'} - {date}</Text>
16 </View>
17 );
18}
19
20const 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

React Native, current, date, example
React Native, get current date example.

Try it yourself

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.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get current month in react native?

How to get current month in react native?

React Native, get current month example.

February 14th, 2022 · 1 min read
How to get current year in react native?

How to get current year in react native?

React Native, get current year example.

February 14th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]