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 8968357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:26:34+00:00 2026-06-15T17:26:34+00:00

I have a user object that is instantiated when a user logs in, this

  • 0

I have a user object that is instantiated when a user logs in, this object stores all the user’s settings:

public class SiteUser
{

    public string LoginId { get; set; }
    public string Name { get; set; }        
    public DateTime PasswordChangedOn { get; set; }
    public string Language { get; set; }
    public string DateFormat { get; set; }
    public string RoleName { get; set; }        
    public ICollection<SitePermission> SitePermissions { get; set; }
}

And a UserContext class that stores and manages the user’s session

   public class UserContext
   {
         public SiteUser SiteUser { get; internal set; }

         public static UserContext Current
         {
                get
                {
                    if (HttpContext.Current == null || HttpContext.Current.Session == null)
                        return null;

                    if (HttpContext.Current.Session["UserContext"] == null)
                        CreateUserContext();

                    return (UserContext)HttpContext.Current.Session["UserContext"];
                }
            }
    }

Now, whenever I have to display a date, be it in a grid, or in a text field, I would always want to reference the DateFormat property of the SiteUser object.

In most of the examples I see, data annotations are used to define the format of the date as such:

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

I have tried modifying it to this:

[DataType(DataType.Date), DisplayFormat(DataFormatString = UserContext.Current.SiteUser.DateFormat, ApplyFormatInEditMode = true)]

But it didn’t work, giving me an error stating that the attribute argument must be a constant.

So what’s the best approach to set the format of my date to my models in an MVC application?

  • 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-15T17:26:35+00:00Added an answer on June 15, 2026 at 5:26 pm

    You could add a property in your view model and use that to render the date rather which would take into account the user specific date and render that in the view instead e.g.

    public string PasswordChangedOnLocal
    {
        get { return PasswordChangedOn.ToString(DateFormat); }
    }
    

    Alternatively, you could just render the date in the view itself using the DateFormat property. This actually gives you a bit more flexibility if wanted to change it later (e.g. went for a system-wide convention) as you wouldn’t need to recompile the site e.g.

    @model SiteUser
    
    ...
    <div id="passwordChangedOn">@Model.PasswordChangedOn.ToString(Model.DateFormat)</div>
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that I save like this in my controller: @user =
I have async methods that returns an objects public static IEnumerable<Users.User> GetUsers(IEnumerable<string> uids, Field
I have an object called users that I use to store user objects that
I have the following code that loads a user's Active Directory DirectoryEntry object from
I have a form that is bind to an object, and when the user
I'm writing an app that monitors the user's location. I have a CLLocationManager object
I have a Ticket class that contains a List of User objects: class Ticket
Following scenario: I have user object: ... @Column(name = TITLE) private String title; @NotNull
I have an app that calls a webservice and retreives a contentValue object. This
I have a PHP file, .../datastuff.php On this page a PHP object is instantiated.

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.