Storing and displaying dates in Umb 17?

I am working on a custom property editor and it will be storing and displaying back a date/time. I plan on using the DateTimePicker built in property but what I have been unable to figure out is how to deal with displaying the time in local? I.E. Save the data as UTC but display in users local zone (GMT, EST, JST, Etc). Does Umbraco have a built in support function for dealing with displaying of saved times or do I need to write up a custom converter?

Hi @Eaglef90

There’s a DateTimeWithTimeZone property editor that displays dates based on the browser timezone. I’ve not used it but it sounds like it does what you need?

Justin

Thanks, I will look into that one.

Wasn’t sure if your question was just about being able to display a time in a set timezone, or if you were after changing from the stored UTC to show the end user (from browser locale, or user profile timezone) so here’s a few thoughts.

Frontend or webcomponent in the backoffice, you can do

const userTime = new Date().toLocaleString(); (javascript)

In c# code.. if you were to do

var now = DateTime.UtcNow;
var local = now.ToLocalTime();
var stringLocal = local.ToString("F");

then you’d end up with the server’s local time..

if you know the users timezone (Umbraco user has set the timezone for instance.. ), and are doing something outside of the usual back office…

DateTime utcNow = DateTime.UtcNow;
TimeZoneInfo userTz = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
DateTime userLocalTime = TimeZoneInfo.ConvertTimeFromUtc(utcNow, userTz);

But AFAIK if you are storing data in a DateTime picker, Umbraco handles the display in the correct user timezone for you. (though never sure which wins if different.. the browser set locale, or the user set timezone property :thinking:)

source code for the picker here.. might help..
Umbraco-CMS/src/Umbraco.Web.UI.Client/src/packages/property-editors/date-time/property-editor-ui-date-time-picker-base.ts at main · umbraco/Umbraco-CMS

I actually reported an issue with the current DateTime implementation. It is now not stored how it used to and the object is actually really odd. PLUS has a few bugs as well.