473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Now.date.tostri ng and now.seconds.tos tring() 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.Ho ur.ToString() + ":" + DateTime.Now.Mi nute.ToString() +
":" + DateTime.Now.Se cond.ToString() +
":"+DateTime.No w.Millisecond.T oString()

to the file.

And then after some small processing i am writing
datetime.now.da te.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 7515
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.Ho ur.ToString() + ":" + DateTime.Now.Mi nute.ToString() +
":" + DateTime.Now.Se cond.ToString() +
":"+DateTime.No w.Millisecond.T oString()
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.ToStrin g() +
":" + dtNow .Second.ToStrin g() +
":"+dtNow .Millisecond.To String();
>
to the file.

And then after some small processing i am writing
datetime.now.da te.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.To String, since
DateTime.Now.Da te.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.ToStrin g() + ":" + dt.Minute.ToStr ing() + ":"
+ dt.Second.ToStr ing() + ":" + 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.Ho ur.ToString() + ":" + DateTime.Now.Mi nute.ToString() +
":" + DateTime.Now.Se cond.ToString() +
":"+DateTime.No w.Millisecond.T oString()

to the file.

And then after some small processing i am writing
datetime.now.da te.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
4899
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 vbscript. I have the following piece of Javascript client code that displays the users clock on the browser and updates it every second.
15
43021
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 mediate between webapps and arbitrary database backends using JDBC. I am very unwilling indeed to write special-case code for particular databases. Our code has worked satisfactorily with many databases, including many instances MS SQLServer 2000...
7
19637
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, Don Following running on client-side:
12
1763
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 2:56:11.987'). This works fine...if you check the value in the table with query analyzer, it shows in there properly. Now, in my app, I'm executing the same query (that I used in QA) and storing tha in a dataview. If I break and watch the program, the...
12
1690
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 but only because I have figured into the equation (manually) the approximate number of leap years. The code reads: Dim TS As New TimeSpan Dim NumDays As Integer
12
2614
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 example: 21/05/2006
15
2343
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 in my code, as it does not seem to work? I am actually trying to learn what parts of the code do what and why, so if anyone can take the time to explain it would be much appreciated. Here is my script: <script language="javascript">
7
2000
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 DateTime.ParseExact and DateTimeFormatInfo but failed, most examples I see convert the final result to a string... I dont want that!
5
19187
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... Here is the simple sample code used to reproduce the problem : DateTime date = DateTime.Now.Date; Response.Write("date.ToString() : " + date.ToString() + "<BR>");
0
9714
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
9594
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
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
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...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
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...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
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.