473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
5 19154
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
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
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
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


"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Harald Hanche-Olsen | last post by:
I'm confused. I was going to try linkchecker, and it dies with a traceback ending in File "/usr/local/lib/python2.4/calendar.py", line 32, in _localized_month _months = AttributeError:...
4
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct...
5
by: ulyses | last post by:
Hi i have got quite strange problem. I wrote programme which shows all runnign processes. This info is get from /proc dir. When a dir that is process dir is found stat file is read and pid, name...
2
by: archana | last post by:
Hi all, I am facing some wired problem while using above mention data type. What i am doing is i am writing DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" +...
2
PEB
by: PEB | last post by:
When I use the statement: Set objDatasheet = Application.Screen.ActiveDatasheet there is no problem on the majority of machines, but on one or two of them Visual Basic generates this kind...
1
by: Christian Heimes | last post by:
Rick King schrieb: datetime.date is a C extension class. Subclassing of extension classes may not always work as you'd expect it. Christian
2
by: Will Rocisky | last post by:
Actually I am trying to save both date and time in one cell but they are given separately by user.
1
by: yawnmoth | last post by:
<a> <b> <c/><c/> </b> <b> <c/><c/><c/> </b> <b> <c/> </b>
1
by: yawnmoth | last post by:
<?php $content = ' <a> <d> <b> <c/><c/> </b> </d> <b> <c/><c/><c/>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.