Try our conversational search powered by Generative AI!

current date (formatted) as default value in a string property

Vote:
 

Hi Guys,

is there a elegant way of getting the current date in a string property when creating a page with this property.

And no the field cannot be a regular DateTime field, because we are using DXC and all simple dates added here are converted to UTC by default so the day changes, and we want the simple date of publishing all over the world.  (lots of threads about this problem) works great in other situations but not when you just want to show the date all over the world.

So what would be a approach to get the date in a string field on creation. ( in a certain format).

[Display(
        GroupName = TabNames.Content,
        Name = "Publication Date",
        Description = "Publication Date of this News Article",
        Order = 9)]
        [DefaultValue(DateTime.Today.ToString("dd-MM-yyyy"))]
        public virtual string PublicationDate {get; set;}
#195701
Aug 05, 2018 14:31
Vote:
 

Hi Vishal,

If you're looking to set a value to a field on creation of the page you can override the SetDefaultValues method as shown below.

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);
    PublicationDate = DateTime.Today.ToString("dd-MM-yyyy");
}

This is then called automatically just the once when the page is created.

#195709
Aug 06, 2018 10:04
Vote:
 

Thanks Paul.. this was too easy and was thinking too complex.. 

#195734
Aug 06, 2018 20:34
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.