没有日期的Javascript解析时间 [英] Javascript parsing Times without Date

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

问题描述

我需要在我的代码中解析和操作没有日期的时间.例如,我可能会从时间选择器中获取字符串15:00".我想将其转换为某种 Time 对象 - 我通常使用 Python 工作,它具有不同的日期、时间和日期时间对象.

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.

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

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"

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

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

推荐答案

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

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

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

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-day or do you mean a duration of time? These are two related, but slightly different concepts.

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

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.

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

Actually, it might not be 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 do want 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.

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

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

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

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解析时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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