Anilkumar iOS Developer
Anilkumar iOS Developer

Reputation: 3755

How to convert string to date formate in react native

I am new to react native, I am getting date as string from server response.

start_date": "2016-05-12", But, I have to display in following format

May 12, 2016

Any suggestions?

Upvotes: 1

Views: 22140

Answers (2)

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

You can use moment.js to perform any date related operation. You can checkout more detail about the moment from here.

Before converting any date which is in string, we need to convert into a date object. After that we can convert it into any formate.

let momentObj = moment('2016-05-12', 'YYYY-MM-DD')
let showDate = moment(momentObj).format('MMM DD, YYYY')

Upvotes: 4

Shashin Bhayani
Shashin Bhayani

Reputation: 1576

I would suggest using a moment.js library.

you can install it with npm install --save moment

usage:

import moment from 'moment';

moment('2016-05-12').format('MMM DD, YYYY');

Read more about moment here: http://momentjs.com/

Upvotes: 2

Related Questions