I'm using Noda Time library in my project to work with Dates. But I need to allow user to enter date/time using DatePicker in specific Noda timezone (non utc, non local/system). How can I achieve that?
Currently I binded my DatePicker to DateTime property and converting this value in property setter to unspecified kind
public DateTime SessionDate
{
get
{
return _sessionDate;
}
set
{
_sessionDate = new DateTime(value.Ticks, DateTimeKind.Unspecified);
OnPropertyChanged("SessionDate");
}
}
So, now I have value entered by user represented as DateTime structure with unspecified kind.
But I need to get the UTC value (Noda Instant) from my unspecified SessionDate by applying known DateTimeZone. I tried to use
var instant = new Instant(SessionDateTime.Ticks);
var offset = myTimeZone.GetUtcOffset(instant);
instant = instant.PlusTicks(- offset.Ticks);
but I'm not sure if this is a good approach
If your user is entering time with respect to a specific time zone, then you're not starting with an
Instant
. You're starting with aLocalDateTime
and aDateTimeZone
. You need to bind those together to get aZonedDateTime
before you can get to anInstant
.