I am using both the native Datetime and the Noda Time library LocalDate in the project. The LocalDate is mainly used for calculation whereas Datetime is used in rest of the places.
Can someone please give me an easy way to convert between the native and nodatime date fields?
I am currently using the below approach to convert the Datetime field to Noda LocalDate.
LocalDate endDate = new LocalDate(startDate.Year, startDate.Month, startDate.Day);
The developers of NodaTime API haven't exposed a conversion from
DateTime
toLocalDate
. However, you can create an extension method yourself and use it everywhere as a shorthand:Usage:
The simplest approach would be to convert the
DateTime
to aLocalDateTime
and take theDate
part:I'd expect that to be more efficient than obtaining the year, month and day from
DateTime
. (Whether or not that's significant for you is a different matter.)In Noda Time 2.0, there's an extension method on
DateTime
so you can use:For the opposite direction, I'd again go via
LocalDateTime
: