By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Comparing input date and system date

P: n/a
I just want to check if a date entered in a textbox is equal to the
current system date. I set the date object from the input field like
this:

dt1=new Date('10/01/2007');

the current system date is retrieved like this:

curDt = new Date();

Lets say that today is same date as 10/1/07. When you view both
variables they are exactly the same, but when i convert the dates to
milliseconds using getTime() function the curDt value has a few more
milliseconds added to it for some reason. So if user entered todays
date the code would never detect if it is todays date. This seems to
be somekind of bug. Im using firefox 2.0.0.7 browser.

if(dt1==curDt) // this will never equal because the system date
always adds a few milliseconds to the date value
if(dt1.getTime==curDt.getTime()) //this will never equal either for
above reason.

has anybody ever seen this bug? This is a bug?

Oct 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 18, 11:27 pm, anagai <andy.na...@gmail.comwrote:
I just want to check if a date entered in a textbox is equal to the
current system date. I set the date object from the input field like
this:

dt1=new Date('10/01/2007');
That would be 12 am;
the current system date is retrieved like this:

curDt = new Date();

Lets say that today is same date as 10/1/07. When you view both
variables they are exactly the same,
That's not so.

They're different objects and they have different values, too.

curDt has the current time.

dt1 has the date, with no time.

but when i convert the dates to
milliseconds using getTime() function the curDt value has a few more
milliseconds added to it for some reason.
Only a few? It must've been very nearl 12am when you made the test.

So if user entered todays
date the code would never detect if it is todays date. This seems to
be somekind of bug. Im using firefox 2.0.0.7 browser.
If you created the date at *exactly* 12 am, you'd get

var dt = new Date("10/18/2007");
var exactlyMidnightICreated = new Date();
dt - exactlyMidnightICreated == 0; // hypothetically true.
if(dt1==curDt) // this will never equal because the system date
always adds a few milliseconds to the date value
if(dt1.getTime==curDt.getTime()) //this will never equal either for
above reason.
dt1=new Date('10/01/2007');
dt2=new Date('10/01/2007');
dt1==dt2; // Well?

Objects aren't equal unless they're the same.

b1=new Boolean(true);
b2=new Boolean(true);
b1==b2;// What do you think?

b1=new Boolean(true);
b2=true;
b1==b2;

Now in the last case, It's my understanding that the expression is
evaluated as:
b1==b2;
1 == true; // interim step. Boolean
true // result.
http://bclary.com/2004/11/07/#a-9.3

11.9.3 The Abstract Equality Comparison Algorithm
1. If Type(x) is different from Type(y), go to step 14.
....
14. If x is null and y is undefined, return true.
15. If x is undefined and y is null, return true.
16. If Type(x) is Number and Type(y) is String, return the result of
the comparison x == ToNumber(y).
17. If Type(x) is String and Type(y) is Number, return the result of
the comparison ToNumber(x)== y.
18. If Type(x) is Boolean, return the result of the comparison
ToNumber(x)== y.

So we a Boolean object, convert it to a number. The result is 1.
Then 1 is compared to true, 1 == true
1 == true; // true.

Oct 19 '07 #2

P: n/a
In comp.lang.javascript message <Xn********************@194.109.133.242>
, Fri, 19 Oct 2007 08:12:27, Evertjan. <ex**************@interxnl.net>
posted:

>Always use standard compliant date time values:

curDt = new Date('2007/10/1');
Pray read Standard EN 28601. Month and Day need 2 digits each.

The truly standards-compliant form would be

curDt = new Date('2007-10-01'); // but that gives NaN.
ASIDE: Numbers are IEEE Doubles, so a NaN is
Sign Bit, 11111111111, 52 bits not all zero.
Does anyone happen to know what 64-bit patterns are
actually used for NaN in different browsers?
>var now = new Date();
dw('now: ' + now + " numeric: " + +now);

var nowMidnight =
new Date(now.getFullYear(),now.getMonth(),now.getDate( ));
dw('nowMidnight: ' + nowMidnight + " numeric: " + +nowMidnight);

var enterdDt = new Date(2007,10-1,19);
dw('enterdDt: ' + enterdDt + " numeric: " + +enterdDt);

var comparing = (+nowMidnight == +enterdDt )
? "Same" : "Different";
:-(

D1 = new Date() ; D1.setHours(0, 0, 0, 0) // start of today, LCT
D2 = new Date( <user input) // Date 00:00:00, LCT
Answer = D1 == D2

Check that <user inputdoes not contain an offset indication such as
GMT or Z etc.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Oct 19 '07 #3

P: n/a
Dr J R Stockton wrote on 20 okt 2007 in comp.lang.javascript:
In comp.lang.javascript message <Xns99CE67D65257Ceejj99@
194.109.133.242>
, Fri, 19 Oct 2007 08:12:27, Evertjan. <ex**************@interxnl.net>
posted:

>>Always use standard compliant date time values:

curDt = new Date('2007/10/1');

Pray read Standard EN 28601. Month and Day need 2 digits each.

The truly standards-compliant form would be

curDt = new Date('2007-10-01'); // but that gives NaN.
So beter not put that on this NG!
You are confusinge the newbees. ;-)

ASIDE: Numbers are IEEE Doubles, so a NaN is
Sign Bit, 11111111111, 52 bits not all zero.
Does anyone happen to know what 64-bit patterns are
actually used for NaN in different browsers?
>>var now = new Date();
dw('now: ' + now + " numeric: " + +now);

var nowMidnight =
new Date(now.getFullYear(),now.getMonth(),now.getDate( ));
dw('nowMidnight: ' + nowMidnight + " numeric: " + +nowMidnight);

var enterdDt = new Date(2007,10-1,19);
dw('enterdDt: ' + enterdDt + " numeric: " + +enterdDt);

var comparing = (+nowMidnight == +enterdDt )
? "Same" : "Different";

:-(

D1 = new Date() ; D1.setHours(0, 0, 0, 0) // start of today, LCT
D2 = new Date( <user input) // Date 00:00:00, LCT
Answer = D1 == D2
Your solution, which we discussed some years ago, I seem to remember,
is the best in my view, but mine hopefully will have the newbees
experimenting and understanding what the numeric value can do.

Check that <user inputdoes not contain an offset indication such as
GMT or Z etc.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Oct 20 '07 #4

P: n/a
dh**********@gmail.com wrote:
On Oct 18, 11:27 pm, anagai <andy.na...@gmail.comwrote:
>dt1=new Date('10/01/2007');

That would be 12 am;
Quite on the contrary, it could mean *anything*.
>the current system date is retrieved like this:

curDt = new Date();

Lets say that today is same date as 10/1/07. When you view both
variables they are exactly the same,
That's not so.

They're different objects and they have different values, too.
That is true in most cases.
curDt has the current time.
It has the current local system time, which is not necessarily the user's
local time.
dt1 has the date, with no time.
That's nonsense, of course. The internal value of Date objects is always
the number of milliseconds since Epoch for the stored date, which means date
and time.
So if user entered todays
>date the code would never detect if it is todays date. This seems to
be somekind of bug. Im using firefox 2.0.0.7 browser.
If you created the date at *exactly* 12 am, you'd get

var dt = new Date("10/18/2007");
var exactlyMidnightICreated = new Date();
dt - exactlyMidnightICreated == 0; // hypothetically true.
>if(dt1==curDt) // this will never equal because the system date
always adds a few milliseconds to the date value
That is nonsense. See below for the correct explanation.
>if(dt1.getTime==curDt.getTime()) //this will never equal either for
above reason.
No, it will never _be_ equal because the first expression evaluates to a
reference to a Function object and the second expression evaluates to a
number value. If the first expression was called instead, there is at least
the possibility that the values can be equal. If your script engine is fast
enough, it is entirely possible that this is the case. If have just tested
it successfully here.
dt1=new Date('10/01/2007');
dt2=new Date('10/01/2007');
dt1==dt2; // Well?

Objects aren't equal unless they're the same.
True.

However, the relational operators "<", "<=", ">", and ">=" can be used to
compare the internal value of Date objects as the operands are implicitly
converted to Number (ES 3, 11.8.5). ToNumber() on Date objects is calling
ToPrimitive() (9.3) which is calling the valueOf() method of those objects
(8.6.2.6) which returns the same value as getTime() (15.9.5.8 and 15.9.5.9).

Therefore, another possibility is

+dt1 == +dt2
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Oct 22 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.