Date function problem

B

bruce

The input for this this code is YearT=2010, MonthIndex=3, DayT = 27,
Start_HourT = 22, Start_MinutesT=15,
End_HourT = 20, End_MinutesT = 15

var InputStart = new Date();
var InputEnd = new Date();

InputStart = Date(YearT, MonthIndex, DayT, Start_HourT,Start_MinutesT,
0,0);
InputEnd = Date(YearT, MonthIndex, DayT, End_HourT, End_MinutesT,0,0);

alert(Start_HourT + ":" + End_HourT); // output is 22:20 Correct
output

alert("==: " + (InputStart == InputEnd)); // Output is true?? Output
Not Correct

How am I "Misusing" JavaScript???

Thanks....
 
B

bruce

If you want to call Date as a constructor, you need to write

  var InputStart = new Date(...);    // note the 'new' operator

Without 'new', Date() will ignore its arguments and return a string
representing the current date/time instead of a Date object.

Thank you. That fixed my problem with the Date constructor.
What am I doing wrong here???

Thank you..

Bruce
 
J

johncoltrane

Le 28/04/10 14:35, bruce a écrit :
Here InputStart and InputEnd are both assigned a Date object with the
time at the moment of assignment :

typeof(InputStart) returns "object", same for InputEnd, you can now use
InputStart.getDay() or any other method of Date.

Here you overwrite the Date object assigned to the InputStart and
InputEnd with what is returned by a call to
Date(like,stefan,wrote,whatever,arguments,you,put,here,won't,be,honoured) :
a string representing the time at the moment of assignment.

typeof(InputStart) returns "string", same for InputEnd.

Because all this happens within a few milliseconds both strings are
equal (the string is rounded to the second) which is why you get "true"
if you test for their equality with (InputStart == InputEnd).
 
R

RobG

The input for this this code is YearT=2010, MonthIndex=3, DayT = 27,
Start_HourT = 22, Start_MinutesT=15,
End_HourT = 20, End_MinutesT = 15

By convention, variable names starting with a capital letter signify
constructors or, where all characters are upper case, constants. Code
is much easier to read if neatly indented and, when posted to a news
group, manually wrapped at about 70 characters.
var InputStart = new Date();
var InputEnd = new Date();

There's no point initialising variables with a value that you
immediately overwrite.
InputStart = Date(YearT, MonthIndex, DayT, Start_HourT,Start_MinutesT,
0,0);
InputEnd = Date(YearT, MonthIndex, DayT, End_HourT, End_MinutesT,0,0);

Here you overwrite the intial values assigned to these variables,
trivialising in the initial assignment. In ECMAScript, variables do
not have a type (their values do), you can assign any type to any
variable so there is no need to initialise them if you don't want to.
alert(Start_HourT + ":" + End_HourT); // output is 22:20 Correct
output

alert("==: " + (InputStart == InputEnd)); // Output is true?? Output
Not Correct

Consider (wrapped for posting):

var yearT=2010,
monthIndex=3,
dayT = 27,
start_HourT = 22,
start_MinutesT=15,
end_HourT = 20,
end_MinutesT = 15,
inputStart = new Date(yearT, monthIndex, dayT,
start_HourT, start_MinutesT, 0, 0),
inputEnd = Date(yearT, monthIndex, dayT, end_HourT,
end_MinutesT, 0, 0);

alert(inputStart == inputEnd); // false
 
B

bruce

By convention, variable names starting with a capital letter signify
constructors or, where all characters are upper case, constants. Code
is much easier to read if neatly indented and, when posted to a news
group, manually wrapped at about 70 characters.




There's no point initialising variables with a value that you
immediately overwrite.


Here you overwrite the intial values assigned to these variables,
trivialising in the initial assignment. In ECMAScript, variables do
not have a type (their values do), you can assign any type to any
variable so there is no need to initialise them if you don't want to.



Consider (wrapped for posting):

  var yearT=2010,
      monthIndex=3,
      dayT = 27,
      start_HourT = 22,
      start_MinutesT=15,
      end_HourT = 20,
      end_MinutesT = 15,
      inputStart = new Date(yearT, monthIndex, dayT,
                            start_HourT, start_MinutesT, 0, 0),
      inputEnd = Date(yearT, monthIndex, dayT, end_HourT,
                      end_MinutesT, 0, 0);

  alert(inputStart == inputEnd); // false

I "Solved" my compare problem by adding ".toString()" to both
InputStart and InputEnd;

Thanks for the help..

Bruce
 

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
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top