I am new to NodaTime and I would like to implement it in my application.
How can I parse a date string in a NodaTime object?
Here I 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 the last part indicating that it is UTC)
My result variable is now 2015-11-06T13:00:00 NZ (+13)
But I'm not sure I'm on the right track.
This is what I really want.
- Convert
dateInput (date string) to a NodaTime object with the following format dd/MM/yyyy - And then use it as UTC, then convert to
long data type, then store it in the database - Then try to restore the saved data, and then use a specific time zone. 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 fragment and this scenario, when the user can enter date say 06/11/2015 , and then, saving it in the database, I need its current time (current user time) to view it. The reason I convert it to long is because I use the Entity Framework.
Is it possible?
source share