Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert moment date to a string and remove the moment object

Tags:

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.

like image 328
Kelvin Avatar asked Aug 22 '17 04:08

Kelvin


People also ask

How do I remove time from moment date?

We can remove the time portion from a date with the startOf method. We pass in a date with the hour and minutes set into the moment function. Then we call startOf with the 'day' argument to get return a moment object with the time set to midnight of the same date.

How do you find the date string from the moment?

js parsing date and time. We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console. log(parsed.

How do I change a moment date to a specific format?

Date Formatting Date format conversion with Moment is simple, as shown in the following example. moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format.

How do you turn a moment into a date?

Convert time If you are creating timestamps using built-in new Date() , you can also use it to create moment objects: const timestamp = new Date(); const momentTimestamp = moment(timestamp); If you want to create a moment object using current date and time, just call moment() without any arguments.


2 Answers

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>
like image 155
kevguy Avatar answered Oct 15 '22 14:10

kevguy


You're trying to call methods that only exist on a javascript Date object. In order to call those methods you'd need to first convert the Moment object into a plain Date object. You can use the .toDate() method on the Moment object to do this.

var plainDate = moment().toDate();
console.log(plainDate.toUTCString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>

However, a more direct way of converting a Moment object to a string is to use the .format() method, which will output as "ISO 8601" standard that looks like 2014-09-08T08:02:17-05:00.

console.log( moment().format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
like image 39
Soviut Avatar answered Oct 15 '22 15:10

Soviut