473,395 Members | 1,466 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,395 software developers and data experts.

Now.date.tostring and now.seconds.tostring() giving different result.

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.

And then after some small processing i am writing
datetime.now.date.tostring() into file So what i am expecting is time
printed by first line should be less than second line.

But i am getting something strange. what i observered is hours and
minutes printed by both line are same. But second's printed by first
lines are sometimes greated than that of by second line.

Can someone tell me why this is happening. Actually time printed by
second line should be greater than time printed by first line. But in
second's part this is not happening.

Can someone tell me why this is happening.

Please help me.

Thanks in advance.

Aug 8 '06 #1
2 7490
archana wrote:
Hi all,

I am facing some wired problem while using above mention data type.
Several comments, see below...
>
What i am doing is i am writing
DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() +
":" + DateTime.Now.Second.ToString() +
":"+DateTime.Now.Millisecond.ToString()
First off, you have a race condition between your thread and the clock.
DateTime.Now is a dynamic property that's always changing. Here, you're
accessing it 4 times with no assurance that the clock didn't "tick" in
between some of those accesses. You should always take a copy of
DateTime.Now and then work with that copy since it's stable.

DateTime dtNow = DateTime.Now;
dtNow .Hour.ToString() +
":" + dtNow .Minute.ToString() +
":" + dtNow .Second.ToString() +
":"+dtNow .Millisecond.ToString();
>
to the file.

And then after some small processing i am writing
datetime.now.date.tostring() into file So what i am expecting is time
printed by first line should be less than second line.
I'm assumiing that you mean DateTime.Now.ToString, since
DateTime.Now.Date.ToString would always print a time of midnight
(00:00:00.000).
>
But i am getting something strange. what i observered is hours and
minutes printed by both line are same. But second's printed by first
lines are sometimes greated than that of by second line.
This is due to the race condition, most likely - your first line actually
printed an incoherent time because the clock "ticked" between some pair of
the 4 separate accesses to DateTime.Now.
>
Can someone tell me why this is happening. Actually time printed by
second line should be greater than time printed by first line. But in
second's part this is not happening.

Can someone tell me why this is happening.
HTH

-cd
Aug 8 '06 #2
Hi arcana,

I can't tell you the exact reason for why the second time would be lower
than the first, but as Carl stated, using DateTime.Now several times to
assemble a single date is bad.

A couple of tips:

As Carl said, store the time in a single DateTime

DateTime dt = DateTime.Now;

string time = dt.Hour.ToString() + ":" + dt.Minute.ToString() + ":"
+ dt.Second.ToString() + ":" + dt.Millisecond.ToString();

Even better, when concatenating strings, if you have a single "" inside,
all objects will be called with ToString automatically

string time = dt.Hour + ":" + dt.Minute + ":" + dt.Second + ":"
+ dt.Millisecond;

Event better, the DateTime class already provides means of formatting your
own dates and times

string time = dt.ToString("HH:mm:ss:ffff");

Read this page on how to format DateTimes
http://msdn2.microsoft.com/en-us/lib...ormatinfo.aspx

On Tue, 08 Aug 2006 07:04:36 +0200, archana <tr**************@yahoo.com>
wrote:
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.

And then after some small processing i am writing
datetime.now.date.tostring() into file So what i am expecting is time
printed by first line should be less than second line.

But i am getting something strange. what i observered is hours and
minutes printed by both line are same. But second's printed by first
lines are sometimes greated than that of by second line.

Can someone tell me why this is happening. Actually time printed by
second line should be greater than time printed by first line. But in
second's part this is not happening.

Can someone tell me why this is happening.

Please help me.

Thanks in advance.


--
Happy Coding!
Morten Wennevik [C# MVP]
Aug 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Harag | last post by:
Hi all I think this is in the wrong group but since I don't read others much or code in java script I was wondering if anyone could help me with this small problem, as I code mostly in ASP...
15
by: Simon Brooke | last post by:
I'm investigating a bug a customer has reported in our database abstraction layer, and it's making me very unhappy. Brief summary: I have a database abstraction layer which is intended to...
7
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance,...
12
by: Rob T | last post by:
I'm storing a date/time into a SQL table of type datetime. I need it to be precise so the value is stored to the 1000th of a second. ie "insert into myTable mydate values ('08/05/2005...
12
by: Woody Splawn | last post by:
I am trying to determine the age of a person based on two dates, the Date of Birth and Today(). I have a function that does this but it is kludgey and is giving me an age that is pretty close...
12
by: Rob Meade | last post by:
Hi all, Ok - I've come from a 1.1 background - and previously I've never had any problem with doing this: Response.Write (Session("MyDate").ToString("dd/MM/yyyy")) So, I might get this for...
15
by: Cerebral Believer | last post by:
Hi all, I am a newbie to JavaScript and am just trying to get a script going that will write the ful date and time to each webpage as it is viewed. Can anyone point out what mistakes there are...
7
by: JamesG | last post by:
Hi, I need to convert the current time to the format "hh:mm:ss" and date to the format "yyyy-MM-dd". The result of both conversions needs to be of System.DateTime format. I've tried using...
5
by: =?Utf-8?B?RmFlc3NsZXIgR2lsbGVz?= | last post by:
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... ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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,...
0
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...
0
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...

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.