Navigate back to the homepage

how to convert string to date in react native?

Infinitbility
React Native
June 21st, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To convert string to date in react native, just use the new Date() method to convert string to date object.

Like the following example, we can convert string to date object, and also we can date inbuilt methods after converting.

1const str = "2022-06-21";
2const date = new Date(str);
3console.log(date);
4// Output
5// Tue Jun 21 2022 05:30:00 GMT+0530 (India Standard Time)

Today, I’m going to show you How do i convert string to date react native, as above mentioned here, I’m going to use the new Date() method to convert string to date.

Let’s start today’s tutorial how do you convert string to date react native?

In this example, we will do

  1. Create a sample string variable.
  2. Use new Date() method
  3. Print the Output on the screen
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 const str = '2022-06-21';
9 const dateResult = new Date(str);
10 console.log(dateResult); // Tue Jun 21 2022 05:30:00 GMT+0530 (India Standard Time)
11 setDate(dateResult);
12 }, []);
13
14 return (
15 <View style={styles.container}>
16 {date && (
17 <>
18 <Text>{'Date object => ' + date}</Text>
19 <Text>{'Date => ' + date.getDate()}</Text>
20 <Text>{'Month => ' + date.getMonth()}</Text>
21 <Text>{'Year => ' + date.getFullYear()}</Text>
22 </>
23 )}
24
25 </View>
26 );
27}
28
29const styles = StyleSheet.create({
30 container: {
31 flex: 1,
32 justifyContent: 'center',
33 backgroundColor: '#ecf0f1',
34 padding: 8,
35 },
36 paragraph: {
37 margin: 24,
38 fontSize: 18,
39 fontWeight: 'bold',
40 textAlign: 'center',
41 },
42});

As above mentioned, we are taken the example of a date string variable, convert it into a date object, and printed it on the screen.

Let’s check the output.

React Native, convert string to date example
React Native, convert string to date example
  • Try it yourself

I hope it’s help you, All the best 👍.

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 check string contains substring in react native?

Example of check string contains another string or not in react native with step by step guide.

June 20th, 2022 · 1 min read

how to convert array to string in react native?

Example of convert array to string in react native with step by step guide.

June 20th, 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]