I know there is DateTimeZone.AtStartOfDay
method, but are there analogs to get start of the year/month? I know the easiest way is to hardcode those to "first day of X and 30 seconds" (which I did LocalDateTime start = LocalDateTime.FromDateTime(new DateTime(2014, 1, 1, 0, 0, 1, DateTimeKind.Utc));
), but I think that's far from best practice
You certainly don't need to go via DateTime
- but to get the start of a year in a particular time zone, I'd just use:
var startOfYear = zone.AtStartOfDay(new LocalDate(year, 1, 1));
Note that this will be useful to get the duration between a ZonedDateTime
and the start of the year. For a period (e.g. 5 months, 2 days, 3 hours) you'd need to just use LocalDateTime
, which won't take the time zone into account. (Period arithmetic just becomes weird in the face of time zones...)
To get a LocalDateTime
you can just use:
var startOfYear = new LocalDate(year, 1, 1).AtMidnight();
... although there's no guarantee that that date/time occurred in any particular time zone (or only occurred once).
See more on this question at Stackoverflow