Problem with ToShortDateString()

  • Thread starter Thread starter Chris Hough
  • Start date Start date
C

Chris Hough

Hi

I am using .NET v2.0 and recently sent an app to a client site that was
working correctly in our office (SQLServer 2005 Express). When setting a
date I use ToShortDateString() on a DateTime object and am getting back a
string in the form '2005-08-31'

However, at the new client site, (SQL Server 2005), the exact same
executable is producing the string '05-08-31', which does not work too well
when I want to set dates before 2000.

Can someone please explain what is happening, and, more importantly, what I
can do to fix it?

Thanks in advance,
Chris Hough
 
Hi

I am using .NET v2.0 and recently sent an app to a client site that was
working correctly in our office (SQLServer 2005 Express). When setting a
date I use ToShortDateString() on a DateTime object and am getting back a
string in the form '2005-08-31'

However, at the new client site, (SQL Server 2005), the exact same
executable is producing the string '05-08-31', which does not work too
well when I want to set dates before 2000.

Can someone please explain what is happening, and, more importantly, what
I can do to fix it?

Thanks in advance,
Chris Hough

What does your client have set up as the short date string in Regional
Settings?
 
Chris Hough said:
Hi

I am using .NET v2.0 and recently sent an app to a client site that was
working correctly in our office (SQLServer 2005 Express). When setting a
date I use ToShortDateString() on a DateTime object and am getting back a
string in the form '2005-08-31'

However, at the new client site, (SQL Server 2005), the exact same
executable is producing the string '05-08-31', which does not work too
well when I want to set dates before 2000.

Can someone please explain what is happening, and, more importantly, what
I can do to fix it?

Forget the ToShortDateString(), as you can format using the ToString() with
ToString("yy-dd-MM") as an example, look it up with Google on how to use the
ToString() and formatting dates, using ToString().

Also, if I recall, the date format for short and long date that will be
returned is machine O/S independent. You can check this by looking at the
Regional and Language settings for the O/S. I think you might be able to
override this with a .Net setting, but that is a maybe.
 
Thank you for the reply.

Can you please tell me how to check what they have set up as the short date
string in Regional Settings?
 
Chris Hough said:
Thank you for the reply.

Can you please tell me how to check what they have set up as the short
date string in Regional Settings?

Control Panel/Regional Options or Regional and Languages on Vista
 
Chris,

As a system is set to by instance a Chinese or Swedish setting (where ISO
are used), then 05 08 31 does mean
31th of August in the year 2005

In all other culture cases the by you showed date time is impossible

Therefore you have to look what culture setting is used, and if that is not
an ISO culture then as Arnold wrote if somebody maybe has played with the
datetimes setting to force a kind of ISO (what it is not what you get
returned).

However I am currious in what kind of application you are working because
for me is clear that 99-05-31 is 31 june 1999

Cor
 
Date formatting is a big problem if your app is supposed to be used in
multiple cultures. I think you should consider using the cultureinfo
class to get some meaning out of the format.

Meanwhile, for a stop-gap solution, using literal string formats like
MM-dd-yyyy is a fine idea.

In my applications i use the literal date-time string cause they're
culture specific

Regard
cyril gupta
 
Cyril,
Meanwhile, for a stop-gap solution, using literal string formats like
MM-dd-yyyy is a fine idea.
Be aware that this can only be used localy in the USA, not even in Canada.

Cor
 
The short date setting for the offending computer is: 6/23/2008 - it appears
to be no different from the computers that are working properly.

I don't see how it can be safe to format the date string myself, as must
this not run the risk of mixing up the day and month? i.e. if I set the date
string in my program to be: '1930-01-02', how does SQL Server know whether
this is January 2nd or February 1st?

What is the tie in between the result from ToShortDateString() and what
happens on the SQL Server end - it seems as though there must be some way
that either .NET 'knows' or that SQLServer 'knows' which is the day and
which is the month. As we have clients in both US and Canada, I need a
comprehensive solution.

Regards,
Chris Hough
 
I don't see how it can be safe to format the date string myself, as must
this not run the risk of mixing up the day and month? i.e. if I set the
date string in my program to be: '1930-01-02', how does SQL Server know
whether this is January 2nd or February 1st?

I don't keep dates or times in any database, I convert them to an integer
index (days from a common start date and minutes from midnight) on the way
in and back to DateTime on the way out. It avoids any issues about how
different databases store dates and times.
 
Chris Hough said:
The short date setting for the offending computer is: 6/23/2008 - it
appears to be no different from the computers that are working properly.

I don't see how it can be safe to format the date string myself, as must
this not run the risk of mixing up the day and month? i.e. if I set the
date string in my program to be: '1930-01-02', how does SQL Server know
whether this is January 2nd or February 1st?

If you have a concern about this, then you need to save the date as sting on
the SQL Server table and format it the way you need the date before you save
it to the table.
 
Chris,

Your server has to a mechanisme where it looks at the culture the server is
running on.

As we are using (Like mostly in Dutch) servers with US operating systems (To
get service pack and updates in the right timeand to prevent translating
errors), I never have any problem with date and times.

The mechanisme between SQL servers is different per server time field as you
probably know. SQL server 2008 is brought more inline with DotNet datetimes
although the old stay.

In fact as you keep far away from globalization settings, this mostly goes
well. As soon as you start toggling those settings wherever, you are mostly
direct or after a while in trouble.

The latter especially by using strings as date times because most people
beside developpers don't know about the different ways date times are
written in this world. I see often that Americans think that there way of
writting is global. However, AFAIK is the American way based on how dates
in English commonly where prononced in the 19th century.

Cor
 
Back
Top