1

I am using DateTimePicker in my react-native app.But when i try to get date in my text field it returns me something like that Monday June 21 2021 17:34:53 GMT.... but I want to get only date which is 2021:06:21 currently i am getting date like that new Date() and in my text I done like that <Text>{date.toString("yyyy-MM-dd")}</Text>

3 Answers3

1

you can do

yourDate.toISOString().slice(0,10).replace(/-/g,"")

in jsx code

<Text>{yourDate.toISOString().slice(0,10).replace(/-/g,"")}</Text>

base this answer

OR

use moment library

<Text>{ moment(date).format('MM-DD-YYYY')}</Text>
Yoel
  • 7,555
  • 6
  • 27
  • 59
  • 1
    at first, you did not introduce the moment library, which I think is a better solution. I have deleted my downvote after your edit. – shad0wec Jun 21 '21 at 12:52
1

Use moment install moment

npm install moment --save   

replace your text tag with this

<Text>{moment(date).format("MMM Do YY")}</Text>

ref : https://momentjs.com/

OR

you can also use toLocaleDateString() to get only date

<Text>{date.toLocaleDateString()}</Text>

OR

You can also convert first your date into string

    <Text>{date.toString().substr(4 ,12)}</Text>
Bisma Azher
  • 689
  • 8
  • 9
  • `In most cases, you should not choose Moment for new projects` - https://momentjs.com/docs/#/-project-status/ (moment is deprecated) – Hagai Harari Jun 21 '21 at 12:52
-2

I would recommend using https://momentjs.com/.

then you can write moment(yourDate).format('MM/DD/YYYY'); or whatever format you want.

shad0wec
  • 335
  • 1
  • 3
  • 15