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

Calculate difference in dates

For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
sdt = new Date(starttime)
starttime= Math.ceil((starttime) / 1000 / 60 / 60 / 24 )
ndt = new Date()

y = sdt.getYear()
m = sdt.getMonth() + 1
d = sdt.getDate()
h = starttime

Thanx
Jul 23 '05 #1
26 4362
Frank wrote on 29 aug 2004 in comp.lang.javascript:
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
sdt = new Date(starttime)
starttime= Math.ceil((starttime) / 1000 / 60 / 60 / 24 )
ndt = new Date()

y = sdt.getYear()
m = sdt.getMonth() + 1
d = sdt.getDate()
h = starttime


A bit young for your son toread the web ;-}

Read the faq:

<http://www.merlyn.demon.co.uk/js-date0.htm#DC>
and
<http://www.merlyn.demon.co.uk/js-date1.htm#diff>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 23 '05 #2
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")


It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I'm certain that the format
above will cause problems in some browsers.

var birth = new Date(2003, 7, 10, 7, 7);

[snip]

Calculating differences in dates is simply a matter of subtracting one
Date object from another. This will yield a number representing the
milliseconds. You can then use a new Date object to return the number of
years, months, days and hours:

var difference = new Date(new Date() - birth),
years = difference.getFullYear() - 1970,
months = difference.getMonth(),
days = difference.getDate() - 1,
hours = difference.getHours(),
t = new String(years);

The adjustments above are based on the fact that a time of zero (0)
represents the date, 01-Jan-1970 00:00:00 GMT. The adjustments make the
respective values zero-based.

t += ' year';
if(1 != years) {t += 's';}
t += ', ' + months + ' month';
if(1 == months) {t += 's';}
t += ', ' + days + ' day';
if(1 == days) {t += 's';}
t += ', and ' + hours + ' hour';
if(1 == hours) {t += 's';}
alert(t);

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
JRS: In article <opsdh37io1x13kvk@atlantis>, dated Sun, 29 Aug 2004
16:01:43, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
For my website i would like to display the age of my son in years,
months, days and hours.

For now i manage to get a result for totals. Like the total number of
days.

This is the beginning:

starttime = Date.parse("Aug 10,2003, 07:07")
It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I'm certain that the format
above will cause problems in some browsers.


Can anyone provide an actual example of failure? ISTM worth settling
the point of whether all javascript systems, however configured, can
read a date with English-MON DD YYYY in arbitrary order and
reasonable punctuation.
var birth = new Date(2003, 7, 10, 7, 7);
Not fully equivalent, though; the first returns a time_t (ms) and the
second a Date Object.

That form uses Month-1, and hence is amenable to human error; I'd
suggest new Date("2003/08/10 07:07") , in which the string looks like
the right date and is as near ISO-8601 as my system accepts. Again, can
anyone find an example of failure of that form?

[snip]

Calculating differences in dates is simply a matter of subtracting one
Date object from another.
No. It does give the absolute time interval.
This will yield a number representing the
milliseconds.
Yes.
You can then use a new Date object to return the number of
years, months, days and hours:
No; *a* number of ...

Consider a boy born 2004-01-01 00:00:00; at 2008-12-31 12:00:00 he will
be nearly 5 years old, and looking forward to his party on the next day.
He will be 1461 + 365.5 days old.

Consider a boy born 1970-01-01 00:00:00; he will be 1461 + 365.5 days
old on 1975-01-01 12:00:00 he will be 5 years old, and looking forward
to his party on that afternoon.
A boy born 2004-02-01 will be a month old on 2004-03-01 ; 29 days.
A boy born 2005-02-01 will be a month old on 2005-03-01 ; 28 days.
Starting from 1970-01-01, 29/28 days is not yet a month.

The lad was 6 months old on 2004-02-10;

starttime = Date.parse("Aug 10,2003, 07:07")
finaltime = Date.parse("Feb 10,2004, 07:07")

D = new Date(finaltime-starttime)

gives me Sat Jul 4 02:00:00 UTC+0100 1970 -> 6 mo 3 dy 2 hr; the
same every year, since the end of Feb real time is not crossed.
Now consider those, of all ages, who were born on March 28th at noon.
They will have been a week old on the following April 4th, at noon. But
in Europe[~] 3/7 of them will then have been an hour younger than the
other 4/7; and probably /vice versa/ in much of the USA.

[~] EU & neighbours; but not Iceland (there may be other exceptions).

Moreover, the lad may be a foreigner. Differences from GMT will be
(too) properly allowed for in determining the interval; but datum is
1970.0 GMT. Your method, which adds diff to datum and then uses the
getFullYear family, gives a result depending on the location of the
answering system. Few places use GMT as civil time year-round.
t += ' year';
if(1 != years) {t += 's';}
t += ', ' + months + ' month';
if(1 == months) {t += 's';}
t += ', ' + days + ' day';
if(1 == days) {t += 's';}
t += ', and ' + hours + ' hour';
if(1 == hours) {t += 's';}
alert(t);


Erroneous pluralisation?

IMHO, the OP needs to step forwards in integer civil years from the DoB
until the next step will pass the present instant, counting them; then
likewise in months, days, hours, and minutes.

He could step backwards; I expect that the answer will sometimes[*]
differ.
[*] Assuming that the OP repeats the test for a statistically-
significant[+] number of sons[#].
[+] Which need not be all his own.
[#] Or daughters, wives, etc.
A further correction, probably of about 9 months, will be needed if the
lad is Korean (4.5 mo if half-Korean?), and the stated date is DoB.
See via below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4
On Mon, 30 Aug 2004 17:19:23 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <opsdh37io1x13kvk@atlantis>, dated Sun, 29 Aug 2004
16:01:43, seen in news:comp.lang.javascript, Michael Winter
<M.******@blueyonder.co.invalid> posted :
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
[snip]
starttime = Date.parse("Aug 10,2003, 07:07")
It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I'm certain that the format
above will cause problems in some browsers.


Can anyone provide an actual example of failure? ISTM worth settling
the point of whether all javascript systems, however configured, can
read a date with English-MON DD YYYY in arbitrary order and
reasonable punctuation.


Would it be possible? As there is no definition on what should result, or
be accepted, in any of the string-related methods, it would be up to the
developers to decide what should be a reasonable format. That said, I
agree: it would be nice to know.
var birth = new Date(2003, 7, 10, 7, 7);


Not fully equivalent, though; the first returns a time_t (ms) and the
second a Date Object.


Yes. Sorry about that.
That form uses Month-1, and hence is amenable to human error;


True. I did mean to describe the arguments. However, once documented, it
shouldn't be of any concern.

So, for that description...

var birth = new Date(
2003, // Year
7, // Month, starting from zero. 0-Jan 1-Feb 2-March ...
10, // Date
7, // Hours
7); // Minutes

[snip]
Calculating differences in dates is simply a matter of subtracting one
Date object from another.


No. It does give the absolute time interval.


Could you explain what you see as the difference? Is it a matter of
semantics, or something more substantial?

[snip]
You can then use a new Date object to return the number of
years, months, days and hours:
[snipped long example]

Hmm. Yes, I see the problem.
t += ' year';
if(1 != years) {t += 's';}
t += ', ' + months + ' month';
if(1 == months) {t += 's';}
t += ', ' + days + ' day';
if(1 == days) {t += 's';}
t += ', and ' + hours + ' hour';
if(1 == hours) {t += 's';}
alert(t);


Erroneous pluralisation?


Unfortunately. When I tested what I wrote, I corrected that sequence but I
didn't copy the changes back to my post. The comparisons should all be
"not equal".

[snip]

It never ceases to amaze me how something as seemingly simple as date
manipulation can turn into such a minefield.

Thank you,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #5
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message news:<opsdh37io1x13kvk@atlantis>...
var birth = new Date(2003, 7, 10, 7, 7);
var difference = new Date(new Date() - birth),
years = difference.getFullYear() - 1970,
months = difference.getMonth(),
days = difference.getDate() - 1,
hours = difference.getHours(),
t = new String(years);

t += ' year';
if(1 != years) {t += 's';}
t += ', ' + months + ' month';
if(1 == months) {t += 's';}
t += ', ' + days + ' day';
if(1 == days) {t += 's';}
t += ', and ' + hours + ' hour';
if(1 == hours) {t += 's';}
alert(t);


Hi Thanks you both for responding.
Jul 23 '05 #6
Hi i'm sorry but this is i think a bit over my head as a newbe.

Do you guys have a complete answer for me, because i got lost.
I really appriciate the input.

Thanx in advance.
Jul 23 '05 #7
On 29 Aug 2004 08:18:46 -0700, fa*****@hotmail.com (Frank) wrote:
For my website i would like to display the age of my son in years,
months, days and hours.


Frank - if you ever get/got this to work could you please post the
code? I've been looking to do the exact same thing for a good two
years now. Every once in a while I seach the net for code but the
best I can find only gives total number of days, months, etc (ie: 30
months, 919 days, etc). In fact this is the very reason I'm reading
this group now!

I don't know anything about javascript so I am completely lost.

Eternally grateful,

-Otter
Jul 23 '05 #8
Hi guys

This won't immediately solve your problems, but it will go a long way to
showing you what's involved. Anyway, it's more fun to do some of the work
yourselves :-)

/////////////////////////////////////////////////////////////////////////
// //
// These functions access the user's local date and time. //
// //
/////////////////////////////////////////////////////////////////////////

//
// This function returns the date.
//
function getDateNow () {
var dayNames = new
Array("Sunday","Monday","Tuesday","Wednesday","Thu rsday","Friday","Saturday"
);
var monthNames = new
Array("January","February","March","April","May"," June","July","August","Sep
tember","October","November","December");
var theDate = new Date;

return dayNames[theDate.getDay()] + ", " + monthNames[theDate.getMonth()]
+ ", " + theDate.getDate();
}

//
// This function returns the time in 12-hour clock format.
//
function getTimeNow () {
var theDate = new Date;

return showZeroFill(showTheHour(theDate.getHours())) + ":" +
showZeroFill(theDate.getMinutes()) + ":" +
showZeroFill(theDate.getSeconds()) + " " + showAMPM(theDate);
}

//
// Mos people using this site are not likely to be familiar with the 24-hour
clock,
// or "military time". So this, used in conjunction with "showAMPM", makes
sure that
// times are described in the more familiar format.
//
function showTheHour (theHour) {
if ((theHour > 0) && (theHour < 13)) {
return theHour;
}
else {
if (theHour == 0) {
return theHour;
}
else {
return (theHour - 12);
}
}
}

//
// Pad the a time value with a 0 to the left, if it is less than 10.
//
function showZeroFill (theValue) {
if (theValue > 9) {
return theValue;
}
else {
return "0" + theValue;
}
}

//
// Return "AM" or "PM" according to the hour of the time.
//
function showAMPM (theDate) {
if (theDate.getHours() < 12) {
return "AM";
}
else {
return "PM";
}
}

On 2004/09/06 12:22, in article 14********************************@4ax.com,
"Otter" <Ot***@nospam.net> wrote:
On 29 Aug 2004 08:18:46 -0700, fa*****@hotmail.com (Frank) wrote:
For my website i would like to display the age of my son in years,
months, days and hours.


Frank - if you ever get/got this to work could you please post the
code? I've been looking to do the exact same thing for a good two
years now. Every once in a while I seach the net for code but the
best I can find only gives total number of days, months, etc (ie: 30
months, 919 days, etc). In fact this is the very reason I'm reading
this group now!

I don't know anything about javascript so I am completely lost.

Eternally grateful,

-Otter


Jul 23 '05 #9
"Otter" <Ot***@nospam.net> wrote:
On 29 Aug 2004 08:18:46 -0700, fa*****@hotmail.com (Frank) wrote:
For my website i would like to display the age of my son in years,
months, days and hours.


Frank - if you ever get/got this to work could you please post the
code? I've been looking to do the exact same thing for a good two
years now. Every once in a while I seach the net for code but the
best I can find only gives total number of days, months, etc (ie: 30
months, 919 days, etc). In fact this is the very reason I'm reading
this group now!

I don't know anything about javascript so I am completely lost.

Eternally grateful,

-Otter

------------------------------------------------------------------------------------------------------------------------

I have a section on my web page called "Clocks." Here's the URL:

http://users.stans.net/kfeiler/stuff/clocks/clocks.htm

It's not exactly what you want, but it's close. "Clocks" are
countdown clocks from the current date and time to some future
date/time. You might use one, for instance, to determine exactly how
long (in years, days, hours, minutes, and seconds) it is until you
retire, or until your birthday, or to the year 10,000, etc.

What you're after is a little different. You want the elapsed time
from some past event (your son's birthday) to the present. I suspect
that you could use most of the Javascript code in Clocks for what you
want, but you'd have to make changes. It's all downloadable. If you'd
like to look at it, check the HEEEELP section for download
instructions.
Regards,
Kent Feiler
www.KentFeiler.com
Jul 23 '05 #10
JRS: In article <opsdj5hefsx13kvk@atlantis>, dated Mon, 30 Aug 2004
18:22:38, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
On Mon, 30 Aug 2004 17:19:23 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:
JRS: In article <opsdh37io1x13kvk@atlantis>, dated Sun, 29 Aug 2004
16:01:43, seen in news:comp.lang.javascript, Michael Winter
<M.******@blueyonder.co.invalid> posted :
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmail.com> wrote:
[snip]
starttime = Date.parse("Aug 10,2003, 07:07")

It would be safer to use numbers, rather than a string as there is no
exact definition for string date formats. I'm certain that the format
above will cause problems in some browsers.


Can anyone provide an actual example of failure? ISTM worth settling
the point of whether all javascript systems, however configured, can
read a date with English-MON DD YYYY in arbitrary order and
reasonable punctuation.


Would it be possible? As there is no definition on what should result, or
be accepted, in any of the string-related methods, it would be up to the
developers to decide what should be a reasonable format. That said, I
agree: it would be nice to know.

Certainly it would be possible. The three-letter abbreviation must be
the month; ignoring case, jan..dec, all else being an error. The four
digits must be the year, so the other field the day.

There is the question of what punctuation is reasonable; in mine, f,eb
gives NaN, but fe,b is OK (and GMT+2); an isolatable letter appears to
be taken as offset (j excluded).

My MSIE4 system accepts
Feb 3 2222 YES
Feb 3st 2222 NO
Feb 3nd 2222 NO
Feb 3rd 2222 NO
Feb 3th 2222 YES
where the last two are alarming.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #11
JRS: In article <14********************************@4ax.com>, dated
Mon, 6 Sep 2004 06:22:12, seen in news:comp.lang.javascript, Otter
<Ot***@nospam.net> posted :
On 29 Aug 2004 08:18:46 -0700, fa*****@hotmail.com (Frank) wrote:
For my website i would like to display the age of my son in years,
months, days and hours.


Frank - if you ever get/got this to work could you please post the
code? I've been looking to do the exact same thing for a good two
years now. Every once in a while I seach the net for code but the
best I can find only gives total number of days, months, etc (ie: 30
months, 919 days, etc). In fact this is the very reason I'm reading
this group now!


Evertjan wrote :-

Read the faq:

<http://www.merlyn.demon.co.uk/js-date0.htm#DC>
and
<http://www.merlyn.demon.co.uk/js-date1.htm#diff>

but perhaps he missed
<URL:http://www.merlyn.demon.co.uk/js-dates.htm#Intro> !

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #12
JRS: In article <BD***********************@btclick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang.javascript, Ian Sedwell
<ia*********@btclick.com> posted :
This won't immediately solve your problems, but it will go a long way to
showing you what's involved. Anyway, it's more fun to do some of the work
yourselves :-)
It seems rather a pointless contribution. Did you write it yourself?

Code posted in News should not be allowed to be line-wrapped by the
posting software; the poster should wrap it by hand himself. Or
herself.
// This function returns the time in 12-hour clock format.
A silly thing to do.

// Mos people using this site are not likely to be familiar with the 24-hour
clock,
Most people here know it well; even the Americans.

function showTheHour (theHour) {
if ((theHour > 0) && (theHour < 13)) {
return theHour;
}
else {
if (theHour == 0) {
return theHour;
}
else {
return (theHour - 12);
}
}
}
ISTM that the above code does not do the customary thing for the first
hour of the day; but I've not run the code.

On 2004/09/06 12:22, in article 14********************************@4ax.com,
"Otter" <Ot***@nospam.net> wrote:


Responses should go after trimmed quotes.

Carefully reading a newsgroup FAQ before posting to the group is
strongly recommended; anyone who posts an answer palpably inferior to
one indicated by the FAQ is liable to be considered, and described as,
injudicious.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 23 '05 #13
On 2004/09/06 20:56, in article Yl**************@merlyn.demon.co.uk, "Dr
John Stockton" <sp**@merlyn.demon.co.uk> wrote:
JRS: In article <BD***********************@btclick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang.javascript, Ian Sedwell
<ia*********@btclick.com> posted :
This won't immediately solve your problems, but it will go a long way to
showing you what's involved. Anyway, it's more fun to do some of the work
yourselves :-)
It seems rather a pointless contribution. Did you write it yourself?


Yes I did.
Code posted in News should not be allowed to be line-wrapped by the
posting software; the poster should wrap it by hand himself. Or
herself.
// This function returns the time in 12-hour clock format.
A silly thing to do.


Please explain that to my client and everyone else who prefers the 12-hour
clock. I prefer the 24-hour clock personally, but then I had it drummed into
my head during flying training.
// Mos people using this site are not likely to be familiar with the 24-hour
clock,
Most people here know it well; even the Americans.


Why should we expect the Americans to have an inferior intelligence?
function showTheHour (theHour) {
if ((theHour > 0) && (theHour < 13)) {
return theHour;
}
else {
if (theHour == 0) {
return theHour;
}
else {
return (theHour - 12);
}
}
}
ISTM that the above code does not do the customary thing for the first
hour of the day; but I've not run the code.


It works very well.
On 2004/09/06 12:22, in article 14********************************@4ax.com,
"Otter" <Ot***@nospam.net> wrote:


Responses should go after trimmed quotes.

Carefully reading a newsgroup FAQ before posting to the group is
strongly recommended; anyone who posts an answer palpably inferior to
one indicated by the FAQ is liable to be considered, and described as,
injudicious.


I hope that no one ever describes you as an arrogant toe-rag. That would be
palpably injudicious.

Jul 23 '05 #14
Ian Sedwell wrote:
function showTheHour (theHour) {
if ((theHour > 0) && (theHour < 13)) {
return theHour;
}
else {
if (theHour == 0) {
return theHour;
}
else {
return (theHour - 12);
}
}
}
function showTheHour (theHour){
return theHour==0?12:theHour<13?theHour:theHour-12;
}

If "theHour" is 0, then it is "12" am, no?
Mick
ISTM that the above code does not do the customary thing for the first
hour of the day; but I've not run the code.

It works very well.
On 2004/09/06 12:22, in article 14********************************@4ax.com,
"Otter" <Ot***@nospam.net> wrote:


Responses should go after trimmed quotes.

Carefully reading a newsgroup FAQ before posting to the group is
strongly recommended; anyone who posts an answer palpably inferior to
one indicated by the FAQ is liable to be considered, and described as,
injudicious.

I hope that no one ever describes you as an arrogant toe-rag. That would be
palpably injudicious.

Jul 23 '05 #15
Lee
Otter said:

On 29 Aug 2004 08:18:46 -0700, fa*****@hotmail.com (Frank) wrote:
For my website i would like to display the age of my son in years,
months, days and hours.


Frank - if you ever get/got this to work could you please post the
code? I've been looking to do the exact same thing for a good two
years now. Every once in a while I seach the net for code but the
best I can find only gives total number of days, months, etc (ie: 30
months, 919 days, etc).


It's tough to find because it's not a reasonable way to
measure time.

The length of "three months" can vary by up to three days,
depending on where that period happens to fall in the
calendar year.

If you want to measure time in months, you shouldn't try
to do so in precision greater than half a month.

Jul 23 '05 #16
On Mon, 06 Sep 2004 15:09:43 -0500, Kent Feiler <zz**@zzzz.com> wrote:
http://users.stans.net/kfeiler/stuff/clocks/clocks.htm

It's not exactly what you want, but it's close.


Thanks for that.

Back when I first tried looking for a solution I found a countdown
timer which I used, then modified after the birth to start counting up
(i literally dont know anything about javascript but was able to
figure out enough to make the necessary changes). The only problem is
that it only counts up in terms of days.

I've since replaced the code with a much more streamlined day counter.

I've looked high and low for a javascript counter which can break down
to years/month/days since an event happened but I have yet to find
one. I am assuming now that it can't be done otherwise I'm sure I
would have come across one by now. Found lots of unanswered requests
for the same by others.

I HAVE seen one in cgi-bin but I know even less about that, and I
don't think my service provider allows their use anyway.
http://www.muquit.com/muquit/softwar...2.6/Count.html

An example of what I'm trying to accomplish appears as the last item
here:
http://www.muquit.com/muquit/softwar.../examples.html

Maybe I'll have to start investigating this option.

-Otter
Jul 23 '05 #17
Mick White <mw******@BOGUSrochester.rr.com> writes:
function showTheHour (theHour){
return theHour==0?12:theHour<13?theHour:theHour-12;
}


Then you could also do:

return ((theHour - 1) % 12) + 1;

which is shorter, although not necessarily faster :)

(Please trim your quotes)
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #18
JRS: In article <BD***********************@btclick.com>, dated Mon, 6
Sep 2004 23:38:44, seen in news:comp.lang.javascript, Ian Sedwell
<ia*********@btclick.com> posted :
On 2004/09/06 20:56, in article Yl**************@merlyn.demon.co.uk, "Dr
John Stockton" <sp**@merlyn.demon.co.uk> wrote:
JRS: In article <BD624545.CDE5%ia*********@btclick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang.javascript, Ian Sedwell
<ia*********@btclick.com> posted :
This won't immediately solve your problems, but it will go a long way to
showing you what's involved. Anyway, it's more fun to do some of the work
yourselves :-)


It seems rather a pointless contribution. Did you write it yourself?


Yes I did.


You would have done better to read, with care, the newsgroup FAQ
instead.

// Mos people using this site are not likely to be familiar with the 24-hour
clock,


Most people here know it well; even the Americans.


Why should we expect the Americans to have an inferior intelligence?


Familiarity and intelligence are different concepts. President Bush may
or may not be of similar intelligence to President Putin; but he is
undoubtedly far less familiar with the Russian language. Pres.P is
probably less familiar with the New York Subways - though, given their
backgrounds, one should not be too sure of that.
function showTheHour (theHour) {
if ((theHour > 0) && (theHour < 13)) {
return theHour;
}
else {
if (theHour == 0) {
return theHour;
}
else {
return (theHour - 12);
}
}
}


ISTM that the above code does not do the customary thing for the first
hour of the day; but I've not run the code.


It works very well.


Perhaps what it should give and what you think it should give are not
the same. I don't think that it gives what I think that it should give.

The code would not be needed if the 24-hour clock was used.

H12 = LZ(1+(H24+11)%12)+ [' am', ' pm'][+(H24>11)]

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #19
Lasse Reichstein Nielsen wrote:
Mick White <mw******@BOGUSrochester.rr.com> writes:
function showTheHour (theHour){
return theHour==0?12:theHour<13?theHour:theHour-12;
}


Then you could also do:

return ((theHour - 1) % 12) + 1;


Yes, you could, but it would be wrong. What if theHour is 0 (zero)?
Mick

Jul 23 '05 #20
In article <U0*****************@twister.nyroc.rr.com>, Mick White
<mw******@BOGUSrochester.rr.com> writes

<snip>
If "theHour" is 0, then it is "12" am, no?
Mick

<snip>

Or 12 pm if your reference point is the previous noon instead of the
next noon.

There is also the problem of 12 noon, which is neither before (ante) nor
after (post) noon.

Altogether, the 24 hour clock as the railways call it is a lot easier to
code.

John
--
John Harris
Jul 23 '05 #21
Mick White <mw******@BOGUSrochester.rr.com> writes:
Lasse Reichstein Nielsen wrote:

Then you could also do:
return ((theHour - 1) % 12) + 1;


Yes, you could, but it would be wrong. What if theHour is 0 (zero)?


Arh, bugger. I always forget that %12 can give results that are not
between 0 and 11. May whoever made that decission be forced to watch
Barney for 24 hours a day until he repents!

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #22
JRS: In article <u0**********@hotpop.com>, dated Tue, 7 Sep 2004
19:19:49, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lr*@hotpop.com> posted :
Mick White <mw******@BOGUSrochester.rr.com> writes:
function showTheHour (theHour){
return theHour==0?12:theHour<13?theHour:theHour-12;
}


Then you could also do:

return ((theHour - 1) % 12) + 1;

which is shorter, although not necessarily faster :)


Yields 0 1 2 12 1 2 11.

Use ((theHour + 11) % 12) + 1;

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #23
JRS: In article <gd********************************@4ax.com>, dated
Tue, 7 Sep 2004 03:53:05, seen in news:comp.lang.javascript, Otter
<Ot***@nospam.net> posted :

I've looked high and low for a javascript counter which can break down
to years/month/days since an event happened but I have yet to find
one. I am assuming now that it can't be done otherwise I'm sure I
would have come across one by now. Found lots of unanswered requests
for the same by others.


Counting in Y M D is not all that meaningful, since months (and years,
and days) vary in length.

By using the newsgroup FAQ, you should have found one which counts in
days and hh:mm:ss until Xmas noon (js-date2.htm#RC); you should be able
to modify that to count up.

Likewise, you could have found js-date1.htm#DYMD, which gives the
difference between two dates in Y M D.

Note that, since months differ in length, with two dates D1 D2, the YMD
that D2 is after D1 does not necessarily match the YMD that D1 is before
D2, on reasonable definitions.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #24
Dr John Stockton wrote:

Yields 0 1 2 12 1 2 11.

Use ((theHour + 11) % 12) + 1;


24:00 hrs?
Mick

Jul 23 '05 #25
JRS: In article <5L*******************@twister.nyroc.rr.com>, dated
Tue, 7 Sep 2004 22:37:53, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted :
Dr John Stockton wrote:

Yields 0 1 2 12 1 2 11.

Use ((theHour + 11) % 12) + 1;


24:00 hrs?


I think I could answer your question, if only I knew what it meant.

In context, theHour is a getHours value, hence an integer in 0..23.

<URL:http://www.merlyn.demon.co.uk/js-date9.htm#1224>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jul 23 '05 #26
Dr John Stockton wrote:
JRS: In article <5L*******************@twister.nyroc.rr.com>, dated
Tue, 7 Sep 2004 22:37:53, seen in news:comp.lang.javascript, Mick White
<mw******@BOGUSrochester.rr.com> posted :
Dr John Stockton wrote:
Yields 0 1 2 12 1 2 11.

Use ((theHour + 11) % 12) + 1;


24:00 hrs?

I think I could answer your question, if only I knew what it meant.

In context, theHour is a getHours value, hence an integer in 0..23.

<URL:http://www.merlyn.demon.co.uk/js-date9.htm#1224>.


OK, but I have seen "24:00" hours.
Mick
Jul 23 '05 #27

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: David Gray | last post by:
Greetings all, Quick newbie type question: I would like to be able to trap non-numerical data entered into a textbox via CTRL+C and/or Shift+Insert. I realise that this data can be...
4
by: Manny Chohan | last post by:
Hi Can anyone tell me how i can calculate yesterday date using asp? Thanks manny
5
by: SimonC | last post by:
Help needed for a Javascript beginner. As above in the subject... i need a javascript to run this, but not in the form of a web-page. I want to calculate it between 2 fields in a database that...
1
by: bradleyc | last post by:
How would you calculate the difference between two dates?
2
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates:...
5
by: infobescom | last post by:
Hi I am wrking on a application where i need to calculate the difference between two dates .. here is the formula i am using ........ Public Function GetNumberOfWorkDays(sStartDate,...
8
by: helpless | last post by:
Access 2003 - I am trying to calculate the difference between a specific date 5/1/2007 (not in a data field) and other dates that ARE in a field called Birth Date. I am trying to have it fill into...
7
by: walt | last post by:
Hello, I have been trying to calculate the difference between two date and display the difference in hours and minutes (HH:MM). I can't get it calculate properly and I can't hours and minutes to...
4
by: lenygold via DBMonster.com | last post by:
I found this example in MYSQL: create table events ( id integer not null primary key , datetime_start datetime not null , datetime_end datetime not null ); insert into events values ( 1,...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.