Equivalent of joda LocalTime getMillisOfDay() in noda

不问归期 提交于 2019-12-12 03:41:33

问题


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.

Is there an equivalent one or must I code my own?


回答1:


In Noda Time 1.x, use the LocalTime.TickOfDay property, and then just divide it by NodaConstants.TicksPerMillisecond to get milliseconds:

LocalTime localTime = ...;
long millis = localTime.TickOfDay / NodaConstants.TicksPerMillisecond;



回答2:


Closest you can get to the number of milliseconds since midnight with out-of-the-box .Net functionality:

dateTime.TimeOfDay.TotalMilliseconds

e.g.

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.

It's a double by the way, so you'll also get fractions of milliseconds. 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();


来源:https://stackoverflow.com/questions/37590559/equivalent-of-joda-localtime-getmillisofday-in-noda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!