Comparing input date and system date

A

anagai

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?
 
D

dhtmlkitchen

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.
 
E

Evertjan.

anagai wrote on 19 okt 2007 in comp.lang.javascript:
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 10th of January or the 1st of October?

the current system date is retrieved like this:

curDt = new Date();

Wrong, that returns current local date/time
Lets say that today is same date as 10/1/07.

The 10th of January or the 1st of October?

[Since only Americans do not see the problem, I will assume the latter.]

Always use standard compliant date time values:

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

or even better:

curDt = new Date(2007,10-1,1);


When you view both variables they are exactly the same,

How would you view them?

try

curDt = new Date(2007,10-1,1);
alert(curDt);
alert(+curDt); // numeric view in microseconds since 1970/1/1

curDt = new Date();
alert(curDt);
alert(+curDt); // numeric view in microseconds since 1970/1/1
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.

Evidently. Computer programming is not about a huch,
but about understanding how things work.
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

Wrong idea: the system datetime is as correct as it is set,

Do you expect the compare to go through at midnight exactly?
if(dt1.getTime==curDt.getTime()) //this will never equal either for
above reason.

No, not for the above reason.
has anybody ever seen this bug? This is a bug?

Certainly it is, it is a bug in your mind.
You losely expect the code to work
without researching whwt it really does.

It is not a javascript bug.

=============

Now for a solution:

<script type='text/javascript'>

function dw(x){document.write(x + '<br><br>')}

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";
dw('comparing: ' + comparing);

</script>
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
, Fri, 19 Oct 2007 08:12:27, Evertjan. <[email protected]>
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 input> does not contain an offset indication such as
GMT or Z etc.
 
E

Evertjan.

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. <[email protected]>
posted:



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?




:-(

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.
 
T

Thomas 'PointedEars' Lahn

That would be 12 am;

Quite on the contrary, it could mean *anything*.
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
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.

That is nonsense. See below for the correct explanation.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top