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

Problem with DateTime matching.

88 64KB
I'm trying to match a certain DateTime value with the current system DateTime. I have a timer and a label on a form. Timer interval is set to 1000. When the form loads, the timer starts ticking. As soon as the current DateTime matches the value of the variable, it shows a message in the label.

When I'm writing the following code, the values don't match even if the current system DateTime is equal to the variable. Label1 isn't showing 'Times Matched':
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         Dim dtmNow As DateTime
  3.         dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#
  4.  
  5.             dtmNow = Now
  6.             If dtmVar = dtmNow Then Label1.Text = "Times Matched"
  7.     End Sub
But when I'm writing the same code by parsing 'Now' into DateTime it's working fine.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  2.         Dim dtmNow As DateTime
  3.         dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#
  4.  
  5.             dtmNow = DateTime.Parse(Now.ToString)
  6.             If dtmVar = dtmNow Then Label1.Text = "Times Matched"
  7.     End Sub
Why is it so? The default format of 'Now' is the same as I have stored in dtmVar variable. So there's no question of format mismatch. Does that mean 'Now' is not actually a DateTime property?
Provided, my O.S. is Windows Vista Ultimate and all date/time settings are set to default.
Regards.
May 7 '10 #1

✓ answered by tlhintoq

DateTime.Now goes down to the 3 digits of milliseconds, regardless of how you display it. So getting an exact match of .Now is going to be (literally) a one in a thousand chance.

You'd be better off taking the time as a string, trimmed to hours, minutes, seconds and comparing that.

DateTime.Now.ToString("HH:mm:ss"); // To get 13:30:45
DateTime.Now.ToString("HH:mm:ss.fff"); // To include miliseconds 13:30:45.678

4 1844
tlhintoq
3,525 Expert 2GB
DateTime.Now goes down to the 3 digits of milliseconds, regardless of how you display it. So getting an exact match of .Now is going to be (literally) a one in a thousand chance.

You'd be better off taking the time as a string, trimmed to hours, minutes, seconds and comparing that.

DateTime.Now.ToString("HH:mm:ss"); // To get 13:30:45
DateTime.Now.ToString("HH:mm:ss.fff"); // To include miliseconds 13:30:45.678
May 7 '10 #2
priyamtheone
88 64KB
Thanks for the idea. It's really helpful. This is what I ended up doing:

Dim dtmNow As DateTime
dim dtmVar as DateTime=#04/28/2010 03:25:00 AM#
dtmNow = (Now.ToString("MM/dd/yyyy hh:mm:ss tt"))
If dtmVar = dtmNow Then Label1.Text = "Times Matched"

The other way is to Parse the current DateTime value as I posted.
May 11 '10 #3
Curtis Rutland
3,256 Expert 2GB
Another simple way to do this is to subtract the two DateTime values, and see how many seconds the resulting TimeSpan object contains.

Expand|Select|Wrap|Line Numbers
  1. DateTime dt1 = new DateTime(2010, 5, 12, 9, 38, 0);
  2. TimeSpan difference = dt1.Subtract(DateTime.Now);
  3. double seconds = System.Math.Abs(difference.TotalSeconds);
If seconds < 1 then it is equivalent.

Edit, sorry for the C# code, but it should be pretty self explanatory in VB.NET.
May 12 '10 #4
priyamtheone
88 64KB
Thank you very much.
May 15 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Eric A. Hall | last post by:
I'm trying to scratch up a whois-like client in perl, and am having a strange problem with matching input characters. Specifically, matching against things like "@" works fine over the network, but...
1
by: Lothar Lemnitzer | last post by:
Dear fellows, I have a problem with matching namespaces in an XML document and an XSLT stylesheet (at least I think that this is the source of trouble). My XML document looks like this: 1...
9
by: Iwan Petrow | last post by:
Hi, I have a string which represent a date as follow: "dd-.MM-.yyyy hh:mm". How could I convert this string to DateTime type (I prefer somehow to set this as a pattern if it is possible because...
11
by: Geoff Jones | last post by:
Hi I have a table that has a column with Date types. I am trying to view certain rows in the table using a DataView. Using the filter, I can view the rows with, for example, the date equal...
4
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I am having a problem formatting a string when the time is in format hh.mm.ss - used in Europe Parse seems ok when the date uses "/" or "." as seperator but I get an exception when time...
2
by: wertqt | last post by:
hi there, im having a slight problem in matching the user input(textbox's text) with the values in one of the columns from a MS Access database. Im using C#.NET. The situation is this : My...
97
by: luap | last post by:
Hello good friends, please i need your help and advice on how i should best programme the problem, if possible a different option to solve it is welcomed. The problem a matching problem, how to...
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... ...
1
by: cypriot | last post by:
Hi. I want to insert data to ms access but I think I have a prob with data types. This is my code. savebutton.addActionListener( new ActionListener() { public void...
1
jkmyoung
by: jkmyoung | last post by:
Hey, I'm trying to find an algorithm to approximately match different substrings within a master list, with some forgiveness on spelling. Say you are allowed 3 errors in the string match:...
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
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
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
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
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.