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

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

问题描述

我需要在我的代码中解析和操作没有日期的时代。例如,我可以从timepicker获取字符串15:00。我想把它变成一个时间对象,我通常使用Python,它具有不同的Date,Time和Datetime对象。

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这样的字符串,因为它需要日期信息。我不想向时代添加任意的日期信息 - 特别是因为日期似乎可以根据日期和地点进行假设,例如夏令时,并且似乎有风险自动将时间转换为给定地区。此外,我想要添加时间,例如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天全站免登陆