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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With