I'm new to NodaTime and I would like to implement it in my application.
How can I parse date string to NodaTime Object?
Here's what I currently have:
var dateInput = "06/11/2015";
var pattern = InstantPattern.CreateWithInvariantCulture("dd/MM/yyyy");
var parseResult = pattern.Parse(dateInput);
var localDate = parseResult.Value;
DateTimeZone tzNZ = DateTimeZoneProviders.Tzdb["Asia/Hong_Kong"];
ZonedDateTime result = localDate.InZone(tzNZ);
my localDate variable is now 2015-11-06T00:00:00Z
(and based on what I read in ISO Format, having Z in last part indicated that it's UTC)
my result variable is now 2015-11-06T13:00:00 NZ (+13)
But I'm not sure if I'm in the right path.
Here's what I really want.
- Convert the
dateInput
(date string) to a NodaTime object with the following formatdd/MM/yyyy
- And then have it as UTC then convert to a
long
data type then save it to the database - Then try to retrieve the saved data then use a specific timezone. Say
Asia/Hong_Kong
Is it possible?
EDIT
var dateInput = "06/11/2015";
var pattern = LocalDatePattern.CreateWithInvariantCulture("dd/MM/yyyy");
LocalDate parseResult = pattern.Parse(dateInput).Value;
DateTimeZone tzHK = DateTimeZoneProviders.Tzdb["Asia/Hong_Kong"];
LocalTime time = GetTimeOfDay();
LocalDateTime localDateTime = parseResult + time;
// change it to UTC then convert it to
// long data type then save it to the database
// methods
private LocalTime GetTimeOfDay()
{
var instant = SystemClock.Instance.Now;
var tz = DateTimeZoneProviders.Tzdb["Asia/Hong_Kong"];
return instant.InZone(tz).TimeOfDay;
}
I have this snippet and this scenario where the user can only input date
say 06/11/2015
then upon saving it to the database, I need it's current time (current time of the user) for viewing purposes. The reason why I am converting it to long
is because I am using Entity Framework.
Is this advisable?