WepApp - DateTime.Now.Date.ToString() gives different results

G

Guest

Hello,

I'm facing a strange problem. We have an Asp.net 2.0 Web Application and
somehow the date format is changing from one client to another even if all
the code is running server-side...

Here is the simple sample code used to reproduce the problem :

DateTime date = DateTime.Now.Date;
Response.Write("date.ToString() : " + date.ToString() + "<BR>");

Here is the result I get on my workstation :
date.ToString() : 08/06/2007 00:00:00

And here is the result another client gets :
date.ToString() : 08/06/2007 0.00.00

Notice how the time is formatted. This is strange to me because the
Date.Now.Date is calculated on the server and the formatting too so how can
the result be different ?

I'm a bit stuck on this, any help or advice is welcome.

Kind regards

Gilles
 
G

Guest

Hello,

I'm facing a strange problem. We have an Asp.net 2.0 Web Application and
somehow the date format is changing from one client to another even if all
the code is running server-side...

Here is the simple sample code used to reproduce the problem :

DateTime date = DateTime.Now.Date;
Response.Write("date.ToString() : " + date.ToString() + "<BR>");

Here is the result I get on my workstation :
date.ToString() : 08/06/2007 00:00:00

And here is the result another client gets :
date.ToString() : 08/06/2007 0.00.00

Notice how the time is formatted. This is strange to me because the
Date.Now.Date is calculated on the server and the formatting too so how can
the result be different ?

I'm a bit stuck on this, any help or advice is welcome.

Kind regards

Gilles

Hi Gilles

Try to check current culture:

Current Culture is <%= CultureInfo.CurrentCulture.Name %>

To fix the problem, use

DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss")
DateTime.Now.ToString(new CultureInfo("en-GB"));

or try to set Culture and UICulture to the same culture in web.config

<globalization culture="en-GB" uiCulture="en-GB" />

Hope it helps
 
G

Guest

Thank you Alexei,

I made some tests by changing my Browser language settings and you are
right my culture changes, affecting the datetime.tostring() result.

This is ok for me as I have visual controls like calendar and numbers which
i want to be formatted to the user selected culture.

Problem happens when passing the date as parameter to sql server. I have a
function which normalizes dates to a common format :

date = date.ToString("yyyy-MM-dd HH:mm:ss");

Problem is that when I set my local settings to a format like "dd/MM/yyyy
H.mm.ss" which gives for example "08.06.2007 0.00.00" and pass it to the
normalize function it returns "08.06.2007 00.00.00 without changing the time
separator.

Any idea ?

Thank you

Gilles
 
G

Guest

Ok I found it out. Even when doing
date = date.ToString("yyyy-MM-dd HH:mm:ss");
the current thread culture is used for the separator. To force the separator
i used following syntax :
date = date.ToString("yyyy'-'MM'-'dd' 'HH':'mm':'ss");

Now it can be passed smoothly to sql as a sql convertible datetime.

Regards

Gilles
 
G

Guest

date = date.ToString("yyyy-MM-dd HH:mm:ss");

Problem is that when I set my local settings to a format like "dd/MM/yyyy
H.mm.ss" which gives for example "08.06.2007 0.00.00" and pass it to the
normalize function it returns "08.06.2007 00.00.00 without changing the time
separator.

Do you mean that

DateTime date = new DateTime().Parse("08.06.2007 0.00.00");
Response.Write(date.ToString("yyyy-MM-dd HH:mm:ss"));

returns:
 
G

Guest

Anon User said:
Do you mean that

DateTime date = new DateTime().Parse("08.06.2007 0.00.00");
Response.Write(date.ToString("yyyy-MM-dd HH:mm:ss"));

returns:
------------------------------------------------
08.06.2007 0.00.00

?

No it returns 08-06-2007 00.00.00 because it still uses the separator from the current thread culture. To force the separator i added '' around the separator
yyyy'-'MM'-'dd' 'HH':'mm':'ss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top