View Full Version : Compare one value againt a set?
PhotoJoe47
11-24-2005, 07:03 AM
I'm still working a function that will tell me the difference in years months and days between two dates.
I am a little bit closer. But I have a question about comparing a variable to a list or set of values. I was trying to do this with the switch case function.
switch(m) // m is a month value 0 for Jan ... 11 for Dec.
{
case [0,2,4,6,7,9,11] :
alert((m+1) + " is a 31 day month");
break;
case 1 :
alert("Feb. is a 28 day month");
break;
case [3,5,8,10] :
alert((m+1) + " is a 30 day month");
break;
default :
alert((m+1) + " is not a month between 0 & 11");
}
I want to compare "m" to three different set of numbers. I was wondering if javascript can do this.
The way I would write this out in english is:
If "m" equal 0 or 2 or 4 or 6 or 9 or 11, do this.
If "m" equal 1 do this.
If "m" equal 3 or 5 or 8 or 10, do this.
I know I could do it with a case for each of the numbers in the sets or even with a bunch of if statements. I was just wanting to do it like I have in other programing languages, if possible.
vwphillips
11-24-2005, 08:16 AM
normally best to use the date object for this sort of thing
small example
<html>
<head>
</head>
<body>
<script>
function NuofDays(date){
d=new Date(date[0],date[1],1,-1);
return d.getDate();
}
var D1=new Date(2005,2,10);
var D2=new Date(2005,3,11);
var D3=D2-D1;
alert(NuofDays([2005,11]));
alert(Math.floor(D3/1000/60/60/24)+'days');
</script>
</body>
</html>
PhotoJoe47
11-24-2005, 09:20 AM
Thank you vwphillips,
But that is not what I'm trying to do. I already know how to compute the number of days between to date objects. Kor pointed that out to me in another thread I had posted about a day counter ( http://www.codingforums.com/showthread.php?t=73010 ).
But the question was about comparing a variable against a set of values. I wanted to find another way to do it with less code.
Now the ideal behind this function that I'm trying to use this code in, is to compute the number of years, months and days between two date objects. Because the number of days per month is not the same each month that is what is making the coding of the function so difficult for me. I got most of it figured out, but the days part. But I will keep working on it.
vwphillips
11-24-2005, 09:23 AM
month is not the same each month that is what is making the coding of the function so difficult for me.
that is why I posted this
function NuofDays(date){
d=new Date(date[0],date[1],1,-1);
return d.getDate();
}
PhotoJoe47
11-24-2005, 09:52 AM
Ok vwphillips,
I guess I have been looking at this stuff to long and my mind is numb.
Would you mind explaining what this line of code is doing?
d=new Date(date[0],date[1],1,-1);
Now this is what I think I know, this creates a new date object, but I don't know what the 4 arguments, that you passing to the date function, are about and what they are doing. Please explain.
vwphillips
11-24-2005, 10:41 AM
if you look at my first post
the function is passed the year and the month
whih results in
new Date(2005,11,1,-1)
as js months start at 0 passing month 11 is read as DEC
the first day of DEC -1hour = the last day of NOV
so returns the days in the month of the month passes and given the year looks after leap years 28/29 days in FEB
In other words, the Date() object returnes automatically the correct date as it invoques the internal computer's clock. There is no need to specify the correct date, leap years an so on.
For instance
alert(new Date(2005,1,29))
which you may think that is 29Feb2005, will return the correct 1Mar2005
PhotoJoe47
11-24-2005, 11:00 AM
Ok,
It is starting to make sense to me. The first argument is the year, second is the month (0-11) the third is the day (1-31) and the fourth is the hour. But why is the year and month part of the date array and the day and hour is not? BTW I tried looking for a reference for the date object but nothing I found gave this type of information. I know the code that you gave me works because I tried it with different years and months. I just want to understand the how's and what for's so that I can make changes to your code example and maybe doing something completely different.
the arguments of the Date() object are
var someday= new Date(yyyy,mm-1,dd[,h,m,s,ms])
red is mandatory
If hour is not specified, the default hour is 0
that means
new Date(2005,0,1,-1)
will return 2004 Dec 31 hour 23.
vwphillips
11-24-2005, 11:07 AM
if the parameters are not specified they default
so new Date(2005);
will result in 1 jan 2005 0 hours 0 minutes 0 seconds ..........
Yes, you are right... all the arguments are mandatory. If no argument, the date is the present date.
PhotoJoe47
11-24-2005, 11:15 AM
Thanks guys,
I'm trying now to use this new knowledge to create the display string I was talking about earlier. I will post the results later and see what you think of it, ok?
vwphillips
11-24-2005, 11:22 AM
... all the arguments are mandatory.
all arguments are NOT mandatory but with none, defaults to current date/tine
if an object/method can be written without it's arguments, than, obviousely, the arguments are manadatory...:)
Date() is a valid object, while setTimeout() is not
vwphillips
11-24-2005, 01:00 PM
... all the arguments are mandatory.
all arguments are NOT mandatory but with none, defaults to current date/tine
ok ok... as you wish ... it is just a matter of interpretation... :)
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.