Kevin Dark
Kevin Dark

Reputation: 476

Entering SQL Records With Dates Uses Wrong Date Format

Using: Entity Framework, Sql Server 2014. My application locale/globalisation is not set (yet).

My problem is this; When dealing with dates all around the application they are in the correct UK date format:- dd/mm/yyyy

However, when I use those dates and enter them in to my SQL database (on a shared hosted SQL Server), they go in in the US format mm/dd/yyyy.

How can I change it so that the dates go in to the database in the UK format? Bear in mind I can't touch the settings on the server.

I'm thinking through the connection string with a locale setting or in web.config, but I can't find anything definitive online that can tell me for sure.

I was thinking that this would sort it out but haven't tried it yet.

<configuration>
  <system.web>
    <globalization
      fileEncoding="utf-8" 
      requestEncoding="utf-8" 
      responseEncoding="utf-8"
      culture="en-GB"
      uiCulture="en-GB"
    />
  </system.web>
</configuration>

EDIT: This is the code generated by EF

  public int CreatedBy { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public Nullable<int> ModifiedBy { get; set; }
    public Nullable<System.DateTime> ModifiedDate { get; set; }
    public bool Enabled { get; set; }
    public Nullable<int> ProductSettingId { get; set; }
    public Nullable<int> ProductAreaMinimumPremiumId { get; set; }
    public int ProductId { get; set; }
    public int StatusId { get; set; }
    public Nullable<System.DateTime> InceptionDate { get; set; }
    public Nullable<decimal> Gross { get; set; }
    public Nullable<decimal> Ipt { get; set; }

I'm having issues with all the DateTime properties.

If the date in the UI/app is 12/01/2017 it will go in the DB as 1st Dec 2017 whereas it should be 12th Jan 2017.

Upvotes: 1

Views: 393

Answers (1)

Kevin Dark
Kevin Dark

Reputation: 476

I'm must apologise everyone! It turns out my issue was with the application culture/locale.

By adding:

 <configuration>
  <system.web>
    <globalization
      culture="en-GB"
      uiCulture="en-GB"
    />
  </system.web>
</configuration>

That worked.

I did run this line in on my DB as well, first so I don't know if both these together sorted it out.

  ALTER LOGIN {{MY LOGIN}} WITH DEFAULT_LANGUAGE=British

Upvotes: 1

Related Questions