Link to home
Create AccountLog in
Avatar of Abdurahman Almatrodi
Abdurahman AlmatrodiFlag for Saudi Arabia

asked on

DateTime in C# with SQL 2008

Dear

I am using C# to save DateTime into SQL 2008. In my application the format is correct dd/MM/yyyy, but when inserted to database it changed to MM/dd/yyyy.

I tried to add in Program.cs:

            CultureInfo ci = new System.Globalization.CultureInfo("en-GB");
            System.Threading.Thread.CurrentThread.CurrentCulture = ci;
            System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

But, it did not work.
I found a solution to change it as DateTime.ToString("s") and worked as I want. But, I don't want to use conver function.

I need to set the default all the time in SQL to be dd/MM/yyyy

How to accomplish this?
And, Why is this happening?

btw, the format of my pc is dd/MM/yyyy

Avatar of nishant joshi
nishant joshi
Flag of India image

if you are inserting the currrent date and time then you can use Date() function in your sql query.

and if you want to formate it then

System.Datetime.Now.toString("dd/mm/YYYY");

ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
gustav is correct, the yyyy/mm/dd format will never cause you a problem, and you wont worry about sqlserver or your code understanding the day as month or month as day.

if you are using Parameters, then just give it as a datetime object, and you wont have to worry about the format anymore.
Avatar of Abdurahman Almatrodi

ASKER


Really, it is not easy to understand the issue in easy way.

Gustav:

This is what I done:
DateTime.ToString("yyyy-MM-dd hh:mm:ss tt")

Is this the best way to deal with datetime?
You can build your SQL in many ways, but the format is right except for the missing dot:

"yyyy-MM-dd hh:mm:ss.tt"

and the final string must be wrapped in single-quotes.

/gustav

Dear

What is the "dot" means?