Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Noda Time, how to create a LocalDateTime using a LocalDate and LocalTime

I have a LocalDate and a LocalTime and would like to simply create a LocalDateTime struct from them. I thought of the following extension method which I believe would be the fastest but for an obscure reason the field localTime.TickOfMillisecond does not exist in the current version of the API. So it does not work.

    public static LocalDateTime At(this LocalDate localDate, LocalTime localTime) {
        return new LocalDateTime(localDate.Year, localDate.Month, localDate.Day, localTime.Hour, localTime.Minute, localTime.Second, localTime.Millisecond, localTime.TickOfMillisecond);
    }

So, am I stuck in the mean time to use:

    public static LocalDateTime At(this LocalDate localDate, LocalTime localTime) {
        return localDate.AtMidnight().PlusTicks(localTime.TickOfDay);
    }

Any advice is appreciated.

like image 364
Erwin Mayer Avatar asked Feb 06 '13 08:02

Erwin Mayer


1 Answers

It's much easier than the way you've got it at the moment - simply use the + operator:

LocalDate date = ...;
LocalTime time = ...;
LocalDateTime dateTime = date + time;
like image 140
Jon Skeet Avatar answered Sep 20 '22 17:09

Jon Skeet