Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  136] [ 1]  / answers: 1 / hits: 75443  / 7 Years ago, tue, august 22, 2017, 12:00:00

I have a React Web App and a React Native Mobile App. When I pass a moment date object from my react web app to my backend, it gets converted to a string somehow and it works with my backend.



When I do it with my react native mobile app, it passes the date as a moment object and it doesn't get converted to a string and it doesn't work.



Is there a way to convert the moment object into a plain string like



Tue May 05 2015 23:59:59 GMT+0800 (HKT)


I tried toString() and toUTCString() and it doesn't work. Thanks.


More From » reactjs

 Answers
79

Use moment().format() to create a formatted string from the date.





console.log(moment().format())

<script src=https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js></script>





But if you're using version 2.1.0+ (link), toString should work:





console.log(moment().toString())
console.log(typeof moment().toString())

<script src=https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js></script>




[#56691] Friday, August 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alekgermanb

Total Points: 616
Total Questions: 86
Total Answers: 105

Location: Aland Islands
Member since Thu, Dec 23, 2021
2 Years ago
;