-1

Building an MVC app in Visual Studio. When I test it (run locally in IIS Express) it shows the datetime.tolongdatestring() format correctly. When I publish the site, it is showing the correct Date, but in a Basic format like February-18-2016 rather than the long format.

Site is published on my web server running IIS 7.0, Server 2K8 Standard. Probably something to do with culture or regional settings but please point me in the right direction....

Below is a portion of my model. As mentioned, the string DisplayDate below displays the proper format with the day of the week when run locally, published site does not. Have tried from multiple locations, platforms, etc.

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

        //Display long date format
        public string DisplayDate { get { return (DateAndTime.ToLongDateString()); } }
tereško
  • 58,060
  • 25
  • 98
  • 150
dave317
  • 754
  • 2
  • 12
  • 30
  • 1
    Have you had a look at this SO post: http://stackoverflow.com/questions/2468312/setting-a-date-format-in-asp-net-web-config-globalization-tag – TheEdge Feb 23 '16 at 01:38

1 Answers1

0

Based on the comment suggested post above I was able to get this working using this command. Notice the Thread Current Culture portion (modified code from above):

[Required]
        [Display(Name = "Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime DateAndTime { get; set; }

        //Display long date format
        public string DisplayDate
        {
            get
            {
                // Sets the CurrentCulture property to U.S. English.
                Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                return (DateAndTime.ToLongDateString());
            }
        }
dave317
  • 754
  • 2
  • 12
  • 30
  • So, given that the comment was the solution I marked this question as a duplicate of the one you used to solve it. – TomTom Apr 02 '16 at 08:56