javascript 没有日期的Javascript解析时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17884586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:59:41  来源:igfitidea点击:

Javascript parsing Times without Date

javascriptdatetimetime

提问by mangecoeur

I need to parse and manipulate Times without Dates in my code. For example i might get the string "15:00" from a timepicker. I want to turn this into a Time object of some kind - I normally work in Python which has distinct Date, Time, and Datetime objects.

我需要在我的代码中解析和操作没有日期的时间。例如,我可能会从时间选择器中获取字符串“15:00”。我想把它变成某种 Time 对象——我通常在 Python 中工作,它有不同的日期、时间和日期时间对象。

However all the solutions i've seen focus on using the Date object. This cannot parse a string like "15:00" since it requires day information. I don't want to add arbitrary Date information to Times - especially since Date appears to make assumptions about things like daylight saving depending on the day and the locale, and there appears to be a risk of it automatically attempting to translate the time into a given locale. Furthermore I want to be able to add times, e.g. "15:00 + 1 hour"

然而,我见过的所有解决方案都专注于使用 Date 对象。这无法解析像“15:00”这样的字符串,因为它需要日期信息。我不想向 Times 添加任意日期信息 - 特别是因为 Date 似乎根据日期和语言环境对夏令时等事情做出假设,并且似乎存在自动尝试将时间转换为时间的风险给定的语言环境。此外,我希望能够添加时间,例如“15:00 + 1 小时”

What is the recommended solution to parse and handle "raw" times not associated to dates?

解析和处理与日期无关的“原始”时间的推荐解决方案是什么?

采纳答案by Matt Johnson-Pint

Unfortunately, there's not a great solution. JavaScript only has a Dateobject, which is probably misnamed since it is really a date+time.

不幸的是,没有很好的解决方案。JavaScript 只有一个Date对象,它可能被错误命名,因为它实际上是一个日期+时间。

One thing you might want to think about deeper - you say you want to work with only time, but do you mean a time-of-dayor do you mean a duration of time? These are two related, but slightly different concepts.

您可能想要更深入地考虑一件事 - 您说您只想使用时间,但是您是指一天中的某个时间还是指一段时间?这是两个相关但略有不同的概念。

For example, you said you might want an operation like "15:00 + 1 hour". Well that would clearly be 16:00 either way. But what about "15:00 + 10 hours"? It would be 25:00 if you are talking about a duration, but it might be 01:00 if you are talking about time-of-day.

例如,您说您可能想要“15:00 + 1 小时”这样的操作。好吧,无论哪种方式,这显然都是 16:00。但是“15:00 + 10 小时”呢?如果您谈论的是持续时间,则可能是 25:00,但如果您谈论的是一天中的时间,则可能是 01:00。

Actually, it might notbe 01:00, since not all days have 24 hours in them. Some days have 23, 23.5, 24.5, or 25 hours, depending on what time zone and whether DST is starting or stopping on that day. So in the time-of-day context, you probably dowant to include a particular date and zone in your calculation. Of course, if you are talking about straight 24-hours days, then this point is irrelevant.

实际上,它可能不是01:00,因为并非所有的日子都有 24 小时。有些日子有 23、23.5、24.5 或 25 小时,具体取决于哪个时区以及 DST 是在当天开始还是停止。因此,在一天中的时间上下文中,您可能确实希望在计算中包含特定的日期和区域。当然,如果你说的是连续 24 小时的一天,那么这一点是无关紧要的。

If you are talking about durations- you might want to look again at moment.js, but not at the momentobject. There is another object there, moment.duration. The reference is here.

如果您在谈论持续时间- 您可能想再次查看moment.js,而不是查看moment对象。那里还有另一个物体,moment.duration。参考在这里

And finally, you might want to consider just using plain javascript to parse out hours and minutes from the time string as numbers. Manipulate the numbers as necessary, and then output a string again. But your question seems like you're looking for something more managed.

最后,您可能想考虑仅使用普通的 javascript 将时间字符串中的小时和分钟解析为数字。根据需要处理数字,然后再次输出字符串。但是您的问题似乎是在寻找更有管理性的东西。

回答by Michael Cole

Here's a moment.js solution for 12 or 24 hour times:

这是 12 或 24 小时时间的 moment.js 解决方案:

moment('7:00 am', ['h:m a', 'H:m']); // Wed Dec 30 2015 07:00:00 GMT-0600 (CST)
moment('17:00', ['h:m a', 'H:m']);   // Wed Dec 30 2015 17:00:00 GMT-0600 (CST)
moment('17:00 am', ['h:m a', 'H:m']);// Wed Dec 30 2015 17:00:00 GMT-0600 (CST)
moment('17:00 pm', ['h:m a', 'H:m']);// Wed Dec 30 2015 17:00:00 GMT-0600 (CST)

http://momentjs.com/docs/#/parsing/string-formats/

http://momentjs.com/docs/#/parsing/string-formats/

回答by MindJuice

I ended up using the following since I was already using momentin my app:

我最终使用了以下内容,因为我已经moment在我的应用程序中使用了:

var str = '15:16:33';
var d = new moment(str, 'HH:mm:ss');

See Moment String+Format docsfor other format strings you can use.

有关您可以使用的其他格式字符串,请参阅Moment String+Format 文档

回答by rhysclay

I had to do this recently for a project but didnt really need to include moment.js, the method I used was to manually parse the time like this:

我最近不得不为一个项目这样做,但实际上并不需要包含moment.js,我使用的方法是像这样手动解析时间:

function parseTime(time) {    
    let timeInt = parseInt(time);
    let minutes = time.substring(3,5);

    // you could then add or subtract time here as needed

    if(time > '12:00') {
         return `${timeInt - 12}:${minutes} PM`;
    } else {
         return `${timeInt}:${minutes} AM`;
    }
}

Use this as an alternative starter if you don't want to use moment. Note this example uses es6 syntax.

如果您不想使用 moment,请将其用作替代启动器。请注意,此示例使用 es6 语法。

回答by Mix Master Mike

Okay, so I know I'm way late to the party. Like 6 years late but this was something I needed to figure out and have it formatted HH:mm:ss (24 hours).

好的,所以我知道我参加派对已经很晚了。就像晚了 6 年,但这是我需要弄清楚的事情,并将其格式化为 HH:mm:ss(24 小时)。

moment().format(moment.HTML5_FMT.TIME_SECONDS); // 17:44:56

You can also pass in a parameter like, 2019-11-08T17:44:56.144.

您还可以传入一个参数,例如,2019-11-08T17:44:56.144

moment('2019-11-08T17:44:56.144').format(moment.HTML5_FMT.TIME_SECONDS); // 17:44:56

https://momentjs.com/docs/#/parsing/special-formats/

https://momentjs.com/docs/#/parsing/special-formats/