Try our conversational search powered by Generative AI!

Grzegorz Wiecheć
Sep 4, 2019
  4017
(12 votes)

Time property

Recently we implemented a new open source add-on advanced-cms/time-property. The package contains model type, editing widget and property renderer used to store and display time between 0-24 hours. Property defined in the model is a Timespan type, so there should be no issues with time zones when saving value. It's using dijit/form/TimeTextBox as an editor in edit view.

Getting started

First install Advanced.CMS.TimeProperty package from Episerver's NuGet feed.

PM> Install-Package Advanced.CMS.TimeProperty

Then add a new TimeSpan property to your content model. The property needs to be annotated with BackingType attribute.

[ContentType(GUID = "AREDS8A41-5C8C-G3PJ-8F74-320ZF3DE8227")]
public class TestPage : PageData
{
    [BackingType(typeof(AdvancedCms.TimeProperty.TimeProperty))]
    public virtual TimeSpan? Time1 { get; set; }
}

Edit view configuration

By default, the editor will be displayed using 12-hour clock time format. To change this you can use TimePropertySettings attribute added on the model. For example, to get 24-hour clock format:

[ContentType(GUID = "AREDS8A41-5C8C-G3PJ-8F74-320ZF3DE8227")]
public class TestPage : PageData
{
    [BackingType(typeof(AdvancedCms.TimeProperty.TimeProperty))]
    [TimePropertySettings(TimePattern = "HH:mm")]
    public virtual TimeSpan? Time1 { get; set; }
}

Then it will be rendered as:

View mode property renderer

The NuGet package also contains a renderer for the view mode. You need to use PropertyFor and TimeSpan tag:

@Html.PropertyFor(x => x.CurrentPage.Time1, new { Tag = "TimeSpan" })

To render time with custom format use DateFormat property:

@Html.PropertyFor(x => x.CurrentPage.Time1, new { DateFormat = "hh:mm tt", Tag = "TimeSpan" })

Sep 04, 2019

Comments

Abhinay
Abhinay Oct 14, 2019 01:53 PM

Hi Grzegorz, I installed this plugin but I am only able to see 12 hour time format - please see below.

Is there any particular way so that I can see 24 hour time format too for "TimeSpan" EPiserver properties?

Regards

nitin anand
nitin anand May 15, 2020 02:43 PM

Hi @Abhinay

you need to decorate your property with

[TimePropertySettings(TimePattern = "HH:mm")]

and rebuild project to see 24 hrs format.

Bartosz Sekula
Bartosz Sekula Dec 2, 2021 07:28 PM

The addon is now available for net5 https://nuget.optimizely.com/package/?id=Advanced.CMS.TimeProperty&v=2.0.0

Josh Salwen
Josh Salwen Mar 18, 2022 11:51 PM

This is great. One note, if you add this to a generic list property, then add the ClientEditor reference

public class HoursRange
{
    #region OpenTime
    [Required]
    [Display(Name = "Opening Time")]
    [BackingType(typeof(TimeProperty))]
    [ClientEditor(ClientEditingClass = "advanced-cms-time-property/TimeEditor")]
    public virtual TimeSpan? OpenTime { get; set; }
    #endregion

    #region CloseTime
    [Required]
    [Display(Name = "Closing Time")]
    [BackingType(typeof(TimeProperty))]
    [ClientEditor(ClientEditingClass = "advanced-cms-time-property/TimeEditor")]
    public virtual TimeSpan? CloseTime { get; set; }
    #endregion
}
public class HoursRangeCollectionEditorDescriptor : CollectionEditorDescriptor<HoursRange>
{ }

Please login to comment.
Latest blogs
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024