How to parse a date string in a NodaTime object?

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?

+5
source share
1 answer

I will answer from a slightly different point of view. You said you convert to long because you are using the Entity Framework. This is probably not necessary.

It looks like you're just trying to round off a calendar date. If there is no specific time (for example, midnight or the beginning of the day), and you want all users to see the same year, month and day, no matter what time zone they are in, then it would be better (IMHO) to store things in these terms throughout the process.

Some will object to this, since the general recommendation is โ€œalways stored in UTC,โ€ but this advice does not linger in two general scenarios:

  • "I have a local date and time, but they are in the future, and I use them for planning."

  • "I just work with a calendar date without any time of the day, it may be past or future, but it is a civil date focused on a person, not a unique point in time."

You are in the second case. So:

  • Use the date type in your database, such as the DATE type available in SQL Server, PostgreSQL, MySQL, Oracle, and most other relational databases.

  • Use the LocalDate type in Noda Time. Do not try to convert it to Instant , LocalDateTime , ZonedDateTime or long .

  • Use the DateTime type (with .Kind == DateTimeKind.Unspecified ) to act as an intermediary between the database and your LocalDate property. This is usually done with the interlocutor properties template, as shown in this answer .

+5
source

Source: https://habr.com/ru/post/1234945/


All Articles