kurniawan26
kurniawan26

Reputation: 823

How to take certain parts of a string in React Native?

I have this string :

Wed May 09 2018 13:10:47 GMT+0700 (WIB)

I want to only pick :

May 09 2018

How can I do that ?

Upvotes: 0

Views: 709

Answers (2)

Roy Wang
Roy Wang

Reputation: 11260

I got this error : date.slice is not a function

What you have is a Date object rather than a string.

You can do

date.toString().slice(4, 15)

Alternatively, if you want a more readable and flexible solution (eg. display a different date format, support different languages etc), you can use the momentjs library:

import moment from 'moment';

const date = new Date();
moment(date).format('MMM DD YYYY');

This comes at the cost of larger bundle size if momentjs was not already a dependency.

Upvotes: 3

Bin Feng
Bin Feng

Reputation: 116

Someone already gave an answer in the comment

I give a complete example:

const str = 'Wed May 09 2018 13:10:47 GMT+0700 (WIB)';
console.log(str.slice(4, 15)); // May 09 2018

Upvotes: 1

Related Questions