Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Faessler Gilles - 08 Jun 2007 09:08 GMT
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
Alexey Smirnov - 08 Jun 2007 09:34 GMT
On Jun 8, 10:08 am, Faessler Gilles
<FaesslerGil...@discussions.microsoft.com> wrote:
> Hello,
>
[quoted text clipped - 22 lines]
>
> 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
Faessler Gilles - 08 Jun 2007 10:18 GMT
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
Faessler Gilles - 08 Jun 2007 10:53 GMT
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
Alexey Smirnov - 08 Jun 2007 11:02 GMT
On Jun 8, 11:18 am, Faessler Gilles
<FaesslerGil...@discussions.microsoft.com> wrote:
> 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

?
Faessler Gilles - 08 Jun 2007 12:10 GMT
> On Jun 8, 11:18 am, Faessler Gilles
> <FaesslerGil...@discussions.microsoft.com> wrote:
[quoted text clipped - 17 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.