有没有一种方法可以存储和操作考虑月份长度、闰年等的时间跨度?
我正要自己写这个,但我知道它必须存在,而且我只是设法避免所有可以引导我找到它的谷歌关键字。
我会寻找类似 DDDMMMYYY 的内容,其中 D、M、Y 是天数、月数和年数。因此,00103000 表示三个月零一天的跨度,或者 000000001 表示一年的跨度。理想情况下,这种格式还有一个标准的应用方法,可以考虑时间跨度计算的所有陷阱,例如一个月中不同的天数、闰年等。
我并不是在寻找一种计算时间跨度的方法两个已知时间戳,正如此处所要求的那样(计算 C# 中的相对时间),我正在寻找类似我可以存储的特定字符串格式的东西,该格式将指示用于从已知日期确定第二个未知日期的时间跨度。
例如,使用上面我虚构的格式:如果我说“计算从 2009 年 9 月 15 日起的日期为 00103000”,将返回“2009 年 12 月 16 日”,即 9 月 15 日之后的三个月零一天。
I was just about to write this myself, but I know this has to exist and I'm just managing to avoid all the Google keywords that would lead me right to it.
I would be looking for something like DDDMMMYYY where D, M, Y are the number of days, months, and years. So 00103000 would indicate a span of three months and one day, or 000000001 would indicate a span of one year. Ideally this format would also have a standard way to apply it that could take into consideration all of the pitfalls of timespan calculation like varying number of days in a month, leap years, etc.
I am not looking for a way to calculate a timespan between two known timestamps, as was asked here (Calculate relative time in C#), i'm looking for something like a specific string format I can store that would indicate a span of time to be used for determining a second unknown date from a known one.
Such as, using my fictitious format above: if I said "calculate what date would be 00103000 from September 15, 2009" would return "December 16, 2009", which is three months and one day after September 15th.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议查看 ISO 8061 格式的持续时间。它不仅易于解析并应用于给定日期,而且是一个众所周知的标准,拥有大量可用资源。
从两个时间点确定持续时间有点棘手,但这只是因为没有应用程序的输入,不清楚 3 月 1 日到 6 月 1 日是 3 个月还是 92 天。尽管如此,该格式可以同样很好地表达,例如 0.25 年。
I would recommend looking at the ISO 8061 format for durations. It is not only easy to parse and apply to a given date, it is a well-known standard with a lot of resources available.
Determining the duration from two points in time is a bit trickier, but only because without input from the application, it is unclear whether 1-March to 1-June is 3 months or 92 days. Nonetheless, the format can express either equally well as well as, for example, 0.25 years.
最简单和最常见的表示形式是数字:时间跨度中的秒数(可能是小数,也可能不是小数,具体取决于您的需要)。许多时间库已经将时间表示为自纪元以来的秒数,因此这使得加法和减法时间琐碎。唯一值得关注的主要问题是溢出(例如2038 年错误)。
The simplest and most common representation of this is a numeric: the number of seconds in the time span (which may or may not be fractional, depending on your needs). Many time libraries already represent time as seconds since the epoch, so this makes addition and subtraction of time trivial. The only major issue of concern is overflows (e.g. year 2038 bug).