By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,421 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 8 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jun 8, 10:08 am, Faessler Gilles
<FaesslerGil...@discussions.microsoft.comwrote:
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

Jun 8 '07 #2

P: n/a
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
Jun 8 '07 #3

P: n/a
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
Jun 8 '07 #4

P: n/a
On Jun 8, 11:18 am, Faessler Gilles
<FaesslerGil...@discussions.microsoft.comwrote:
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:
------------------------------------------------
08.06.2007 0.00.00

?

Jun 8 '07 #5

P: n/a


"Alexey Smirnov" wrote:
On Jun 8, 11:18 am, Faessler Gilles
<FaesslerGil...@discussions.microsoft.comwrote:
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:
------------------------------------------------
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
Jun 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.