简体   繁体   English

有效地将大于59(秒)的值转换为DateTime

[英]Converting a value of more than 59 (seconds) effectively into a DateTime

I am getting 'time played' values which come as an integer in the amount of seconds. 我正在获取“时间播放”值,该值以秒为单位作为整数。

Providing there is less than 60 seconds to be added, it's ofcourse easily done. 只要添加少于60秒,就可以轻松完成。

However, a lot of the values are more than 59 seconds and therefore cannot easily be converted into a DateTime. 但是,许多值超过59秒,因此无法轻松转换为DateTime。

An example of a value would be: 159 , which means that the 'time played' is 2 minutes and 39 seconds. 值的示例为: 159 ,这意味着“播放时间”为2 minutes and 39秒。

Thanks in advance. 提前致谢。

A "number of seconds" shouldn't be stored in a DateTime in the first place. “秒数”不应首先存储在DateTime中。 You should use TimeSpan , which makes it easy: 您应该使用TimeSpan ,这很容易:

TimeSpan ts = TimeSpan.FromSeconds(159);

You can add a duration of time to any DateTime , of course, but that's a different matter. 您当然可以为任何DateTime 添加持续时间,但这是另一回事。

(You might also want to look into my Noda Time library if you're doing any significant amount of work with dates and times... the BCL provision is somewhat underwhelming.) (如果您要对日期和时间进行大量工作,则可能还需要查看我的Noda Time库... BCL的规定有些不足。)

I think you want to use a TimeSpan not a DateTime . 我认为您想使用TimeSpan而不是DateTime Specifically TimeSpan.FromSeconds() . 特别是TimeSpan.FromSeconds()

See: http://msdn.microsoft.com/en-us/library/system.timespan.fromseconds.aspx 请参阅: http : //msdn.microsoft.com/en-us/library/system.timespan.fromseconds.aspx

您想要的是一个TimeSpan结构。

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

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