With NodaTime, how do I format ZonedDateTime in the current culture?

I have a ZonedDateTime, and I want to display it so that the datetime is formatted with a short date and a short time configured on the workstation, followed by an offset (something like ... 05/01/2005 14:30 PM - 5:00). I was expecting something like this to work ...

var patternDateTimeOffset = ZonedDateTimePattern.CreateWithCurrentCulture("g o<m>", DateTimeZoneProviders.Tzdb); lblOriginalDateTimeAndOffsetVal.Text = patternDateTimeOffset.Format(zonedDateTime); 

BUT, it seems that ā€œgā€ is not supported in ZonedDateTimePattern as it is in LocalDateTimePattern. In the above code, a NodaTime.Text.InvalidPatternException is thrown.

I could replace "g" with "MM / dd / yyyy hh: mm", but then he did not use the current culture.

I could use LocalDateTimePattern for datetime and then combine the offset using ZonedDateTimePattern. It works, but it seems ugly.

This seems pretty common. I am new to NodaTime, so I'm sure something is missing. I am using NodaTime 1.3.1 and targeting .net 4.0. Any help is appreciated.

+5
source share
1 answer

g works great as a standard template pointer - but only on its own; it cannot be part of the template you are trying to do here. You are effectively trying to mix and match what we do not support :(

Like the parameters you already described (I agree, but will be somewhat ugly), you can use

 var bclDateFormat = CultureInfo.CurrentCulture.DateTimeFormat; var localDateTimePattern = bclDateFormat.ShortDatePattern + " " + bclDateFormat.ShortTimePattern; var patternDateTimeOffset = ZonedDateTimePattern.CreateWithCurrentCulture( localDateTimePattern + " o<m>", DateTimeZoneProviders.Tzdb); 

Still not terribly nice, though, but what is effective is what g does in any case (it uses two existing short patterns and just runs through them).

As Matt said, please write a feature request - I'm not sure the best approach is here, but I'll think about it.

+6
source

All Articles