简体   繁体   English

格式化Moment.js日期时间

[英]Formatting momentjs date-time

Hi everyone I have looked at a few SO questions but have yet to find a solid answer to my problem. 大家好,我看过几个SO问题,但还没有找到我问题的可靠答案。 Question I have already looked at include: 我已经看过的问题包括:

My question is the following: 我的问题如下:

I have a basic time selector with times ranging from 4:00am - 8:00pm. 我有一个基本的时间选择器,时间在4:00 am-8:00 pm之间。 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())

Where filterText is the string passed in by the select: "4:00AM" 其中filterText是select传递的字符串: "4:00AM"

How can i construct something that looks like: "2015-05-09T04:00:00" ? 我如何构造类似"2015-05-09T04:00:00"

As of now I am getting the following, which is way off: "2015-02-27T08:20:00+00:00" 到目前为止,我得到的是以下内容: "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

You can use this: 您可以使用此:

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

You should specify a format string when parsing : 解析时应指定格式字符串

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

From the moment.js docs 从moment.js文档

Warning: Browser support for parsing strings is inconsistent. 警告:浏览器对字符串的支持不一致。 Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers. 因为没有关于应该支持哪种格式的规范,所以在某些浏览器中有效的格式在其他浏览器中无效。

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format. 为了获得一致的解析ISO 8601字符串以外的结果的结果,应使用字符串+格式。

If you don't pass a format string, you may get the correct results, or maybe not. 如果不传递格式字符串,则可能会得到正确的结果,也可能不会。 You will see the following message in the console log: 您将在控制台日志中看到以下消息:

Deprecation warning: moment construction falls back to js Date. 弃用警告:构建时间回落到js日期。 This is discouraged and will be removed in upcoming major release. 不建议这样做,并将在即将发布的主要版本中将其删除。 Please refer to https://github.com/moment/moment/issues/1407 for more info. 请参阅https://github.com/moment/moment/issues/1407了解更多信息。

Also, you probably don't need to pass the time zone for this purpose. 同样,您可能不需要为此目的传递时区。 However, if you id need it to be time zone specific, that would be the wrong way to do it. 但是,如果您需要将其设置为特定于时区,那将是错误的方法。 The moment function doesn't take a time zone parameter. moment函数不采用时区参数。 You would need to use the moment-timezone addon, and pass it to the tz function, like this: 您将需要使用moment-timezone插件,并将其传递给tz函数,如下所示:

time = moment.tz("2015-05-09 " + filterText, "YYYY-MM-DD hh:mmA", "America/New_York");
console.log(time.format("YYYY-MM-DDTHH:mm:ss"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM