如何将当前时间设置为一个值
我只是想知道是否有办法获取当前时间并将其设置为一个值。
如果是 12:06 AM.. 我想获取该时间并将其设置为 currentTime。
例子
float currentTime = 0;
currentTime = 12.06;
I was just wondering if there is a way to get the current time and set it into a value.
If its 12:06 AM.. I want to get that time and set it into currentTime.
Example
float currentTime = 0;
currentTime = 12.06;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如其他人提到的,DateTime 类非常适合此目的,并计算出 2 个日期/时间之间的差异:
DateTime 对象的减法会产生一个 TimeSpan 对象,您可以使用它来查看小时/分钟等。
As others have mentioned, the DateTime class would be ideal for this, and to work out the difference between 2 date/times:
The subtraction of DateTime objects results in a TimeSpan object that you can use to see the hours/minutes etc.
尝试 DateTime 类
try DateTime class
这是您要找的吗?
Is this what you're looking for?
不要使用浮点数或字符串。您可以使用 DateTime 做各种很酷的事情。
您可以通过以下方式获取某人的工作时间:
Don't use floats or strings. You can do all kinds of cool things using DateTime.
Here's how you'd get the hours that someone worked:
有几个人提到了如何操作,但作为“更好”的建议,您应该使用
否则,如果您的计时代码在那些日子运行,那么当时钟倒转时您会遇到问题。 (另外,将 UTC 时间更改为本地时间比将“凌晨 1 点”转换为 UTC 容易得多(因为当时钟倒转时将出现两个)
A few people have mentioned how, but as a 'better' recommendation you should use
Otherwise you have issues when the clocks go back, if your timing code is run on those days. (plus it is far easier to alter the UTC time to local time than it is to convert a '1am' to UTC (as there will be two of them when the clocks go back)
好吧,如果您真的将其视为浮点数,请尝试:
我不建议将日期或时间与浮点数进行比较。更好的选择是使用时间跨度。
Well if you really what it as a float then try:
I wouldn't recommend comparing dates or time with floats. A better options would be to use timespans.
您应该使用
Timespan
实例来获取与时间相关的值,您可以使用灵活性来获取所需的值,例如您可以使用
ts.TotalHours
这将为您提供小数小时(作为double
),否则你可以专门使用ts.Hours
..ts.Minutes
来构造一个字符串,它可能会被证明是有用的。You should be using a
Timespan
instance for time related values, you can use the flexibility to get the required values likeYou could then use
ts.TotalHours
which would give you fractional hours (as adouble
) else you could construct a string specifically usingts.Hours
..ts.Minutes
play around and it could be prove useful.请尝试以下操作:
在
dt
中,您将获得一个工作时间段。Try the following:
In
dt
you will get a working time period.如果你想得到两个时间之间的差异,那么可以这样做:
If you want to have the difference between two times, then do this: