如何将时刻日期转换为字符串并删除时刻对象 [英] How to convert moment date to a string and remove the moment object

查看:79
本文介绍了如何将时刻日期转换为字符串并删除时刻对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React Web App和一个React Native Mobile App.当我从我的React Web应用程序向我的后端传递一个即时日期对象时,它会以某种方式转换为字符串,并且可以与我的后端一起使用.

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.

当我使用React Native移动应用程序执行此操作时,它会将日期作为矩对象传递,并且它不会转换为字符串,因此不起作用.

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)"

我尝试了toString()和toUTCString(),但它不起作用.谢谢.

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

推荐答案

使用 moment().format() 从日期创建格式化的字符串.

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>

但是,如果您使用的是2.1.0+版本(链接 ),toString应该可以工作:

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>

这篇关于如何将时刻日期转换为字符串并删除时刻对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆