[英]Equivalent of joda LocalTime getMillisOfDay() in noda
I am porting some code from Java to .NET and looking for a noda-time equivalent of the getMillisOfDay()
joda-time method of the LocalTime object. 我正在将一些代码从Java移植到.NET,并寻找与LocalTime对象的
getMillisOfDay()
joda-time方法等效的noda-time方法。
Is there an equivalent one or must I code my own? 是否有同等学历,或者我必须自己编写?
In Noda Time 1.x, use the LocalTime.TickOfDay
property, and then just divide it by NodaConstants.TicksPerMillisecond
to get milliseconds: 在Noda Time 1.x中,使用
LocalTime.TickOfDay
属性,然后将其除以NodaConstants.TicksPerMillisecond
即可得到毫秒:
LocalTime localTime = ...;
long millis = localTime.TickOfDay / NodaConstants.TicksPerMillisecond;
Closest you can get to the number of milliseconds since midnight with out-of-the-box .Net functionality: 借助开箱即用的.Net功能,您可以最接近午夜以来的毫秒数 :
dateTime.TimeOfDay.TotalMilliseconds
eg 例如
double millisOfDay = DateTime.Now.TimeOfDay.TotalMilliseconds;
TimeOfDay
returns the TimeSpan
since midnight (time of day) and TotalMilliseconds
returns (the name might have given it away) the total number of milliseconds of that TimeSpan
. TimeOfDay
返回午夜以来的TimeSpan
(一天中的时间), TotalMilliseconds
返回(该名称可能已经给出)该TimeSpan
的毫秒总数。
It's a double
by the way, so you'll also get fractions of milliseconds. 顺便说一句,它是
double
的,因此您还将获得毫秒级的分数。 If you need this a lot, an extension method may be helpful: 如果您非常需要此方法,则扩展方法可能会有所帮助:
public static class DateTimeExtension
{
// should of course be in pascal case ;)
public static int getMillisOfDay(this DateTime dateTime)
{
return (int) dateTime.TimeOfDay.TotalMilliseconds;
}
}
int millisOfDay = DateTime.Now.getMillisOfDay();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.