格式化Moment.js日期时间 [英] Formatting momentjs date-time

查看:85
本文介绍了格式化Moment.js日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我看过几个SO问题,但是还没有找到解决我问题的可靠答案.我已经看过的问题包括:

Hi everyone I have looked at a few SO questions but have yet to find a solid answer to my problem. Question I have already looked at include:

我的问题如下:

我有一个基本的时间选择器,时间在4:00 am-8:00 pm之间.我想使用选择的时间和一个硬编码的日期来格式化如下所示的内容:

I have a basic time selector with times ranging from 4:00am - 8:00pm. I want to use the time selected along with a hard-coded date to format something that looks like this:

"2015-05-03T04:00:00"

这是我到目前为止的内容:

Here's what I have so far:

time = moment("2015/05/09 " + filterText, "America/New_York")
console.log(time.format())

其中filterText是由选择传递的字符串:"4:00 AM"

Where filterText is the string passed in by the select: "4:00AM"

我如何构造看起来像这样的东西:"2015-05-09T04:00:00" ?

How can i construct something that looks like: "2015-05-09T04:00:00" ?

截至目前,我得到的是以下内容:"2015-02-27T08:20:00 + 00:00"

As of now I am getting the following, which is way off: "2015-02-27T08:20:00+00:00"

当我删除时区时,我得到的距离更近,但是时间没有改变:

When I remove the timezone I get something that is closer, but the time is not changing:

time = moment("2015/05/09 " + filterText)
console.log(time.format())
"2015-05-09T08:00:00-07:00" <--- remains at 7:00 even if filterText is 8:00 or 9:00

推荐答案

您可以使用:

time = moment("2015/05/09 " + filterText);
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));

这篇关于格式化Moment.js日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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