473,811 Members | 3,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WepApp - DateTime.Now.Da te.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.Da te;
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 19187
On Jun 8, 10:08 am, Faessler Gilles
<FaesslerGil... @discussions.mi crosoft.comwrot e:
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.Da te;
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.Cur rentCulture.Nam e %>

To fix the problem, use

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

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

<globalizatio n 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.tostri ng() 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.mi crosoft.comwrot e:
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().Pars e("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.mi crosoft.comwrot e:
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().Pars e("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
2547
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: 'module' object has no attribute 'date' Sure enough, there is no datetime.date, but there is a datetime.Date: Python 2.4 (#2, Feb 19 2005, 20:35:23)
4
2435
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 ("2004-11-29") but it's wrong in Internet Explorer 6SP1 ("00:20:15-11-29"). If I change "dateParts" to "dateParts", it's exactly the opposite that occures: a correct result in IExplorer but a fault in Firefox. Is there a workaround? Where do I miss the...
5
1764
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 and state are get from it. The thing is that on my machine every time I run it, it gives different results. Sometimes it shows nothing - most of the times. Sometimes it shows info about 4 processes. Sometimes it works correctly. I don't now whats...
2
7516
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() + ":" + DateTime.Now.Second.ToString() + ":"+DateTime.Now.Millisecond.ToString() to the file.
2
1342
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 of error: Out of Memory On the same kind of machine the code runs normally. I swaped the RAM but there was no result!
1
1415
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
11127
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
1387
by: yawnmoth | last post by:
<a> <b> <c/><c/> </b> <b> <c/><c/><c/> </b> <b> <c/> </b>
1
2133
by: yawnmoth | last post by:
<?php $content = ' <a> <d> <b> <c/><c/> </b> </d> <b> <c/><c/><c/>
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.