Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Shralp 6 posts 76 karma points
    Feb 09, 2023 @ 19:49

    Is there a way to have the builtin Date Picker with time component default to current datetime?

    Title kinda says it all, currently if I have a date time picker on one of my content templates you have to manually select the date and time you want but I'd like it to default to DateTime.Now. This would be especially handy for things like making blog posts where you want to timestamp em on save/publish.

    *Note: I'm using uSkinned Site Builder for my site


  • Johannes Lantz 156 posts 838 karma points c-trib
    Feb 09, 2023 @ 20:49
    Johannes Lantz


    That is possible! You're going to need to subscribe to INotificationHandler<SendingContentNotification>! It could look something like this:

    using System;
    using System.Linq;
    using Umbraco.Cms.Core.Events;
    using Umbraco.Cms.Core.Models.ContentEditing;
    using Umbraco.Cms.Core.Notifications;
    using Umbraco.Extensions;
    namespace Umbraco.Docs.Samples.Web.Notifications
        public class EditorSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
            public void Handle(SendingContentNotification notification)
                if (notification.Content.ContentTypeAlias.Equals("blogpost"))
                    // Access the property you want to pre-populate
                    // each content item can have 'variations' - each variation is represented by the `ContentVariantDisplay` class.
                    // if your site uses variants, then you need to decide whether to set the default value for all variants or a specific variant
                    // eg. set by variant name:
                    // var variant = notification.Content.Variants.FirstOrDefault(f => f.Name == "specificVariantName");
                    // OR loop through all the variants:
                    foreach (var variant in notification.Content.Variants)
                        // Check if variant is a 'new variant'
                        // we only want to set the default value when the content item is first created
                        if (variant.State == ContentSavedState.NotCreated)
                            // each variant has an IEnumerable of 'Tabs' (property groupings)
                            // and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
                            // find the first property with alias 'publishDate'
                            var pubDateProperty = variant.Tabs.SelectMany(f => f.Properties)
                                .FirstOrDefault(f => f.Alias.InvariantEquals("publishDate"));
                            if (pubDateProperty is not null)
                                // set default value of the publish date property if it exists
                                pubDateProperty.Value = DateTime.UtcNow;

    (I copy pasted this code from the documentation! :P ) You can read more about it here:


Please Sign in or register to post replies

Write your reply to: