Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8889033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:09:45+00:00 2026-06-14T22:09:45+00:00

I got a date format like: [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = {0:MM/dd/yyyy})] public DateTime?

  • 0

I got a date format like:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? CreatedOn { get; set; }

Now, I want to make every datetime format in my application read from one config file. like:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = SomeClass.GetDateFormat())]
public DateTime? CreatedOn { get; set; }

but this will not compile.

How can I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:09:47+00:00Added an answer on June 14, 2026 at 10:09 pm

    The problem with this is .NET only allows you to put compile-time constants in attributes. Another option is to inherit from DisplayFormatAttribute and lookup the display format in the constructor, like so:

    SomeClass.cs

    public class SomeClass
    {
        public static string GetDateFormat()
        {
            // return date format here
        }
    }
    

    DynamicDisplayFormatAttribute.cs

    public class DynamicDisplayFormatAttribute : DisplayFormatAttribute
    {
        public DynamicDisplayFormatAttribute()
        {
            DataFormatString = SomeClass.GetDateFormat();
        }
    }
    

    Then you can use it as so:

    [DynamicDisplayFormat(ApplyFormatInEditMode = true)]
    public DateTime? CreatedOn { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got date, stored in MySQL column like this: 09-11-2012 (format is DD/MM/YYYY ),
I've got a String of a date/time in ISO-8601 format, like: 2011-04-15T20:08:18Z I want
So I've got a text input that get's date & time in this format:
ive got 2 strings, date:27.03.11 and time:15:04, which id like to format as a
I can format the Get-Date cmdlet no problem like this: $date = Get-Date -format
I've got a date variable that looks like this: Dim LogDate As Date =
I have a text like: I've got a date with this fellow tomorrow. Well
I've got a one-liner like this: date +%H | perl -ne 'printf %02d, $_
I got the JSON from apple like this { original-purchase-date-pst = 2012-06-28 02:46:02 America/Los_Angeles;
I've got a spreadsheet like this: date | 7/1 | 7/2 | 7/3 |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.