473,782 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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((star ttime) / 1000 / 60 / 60 / 24 )
ndt = new Date()

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

Thanx
Jul 23 '05
26 4416
JRS: In article <opsdj5hefsx13k vk@atlantis>, dated Mon, 30 Aug 2004
18:22:38, seen in news:comp.lang. javascript, Michael Winter <M.Winter@bl
ueyonder.co.inv alid> posted :
On Mon, 30 Aug 2004 17:19:23 +0100, Dr John Stockton
<sp**@merlyn.d emon.co.uk> wrote:
JRS: In article <opsdh37io1x13k vk@atlantis>, dated Sun, 29 Aug 2004
16:01:43, seen in news:comp.lang. javascript, Michael Winter
<M.******@bluey onder.co.invali d> posted :
On 29 Aug 2004 08:18:46 -0700, Frank <fa*****@hotmai l.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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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.n et> 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.demo n.co.uk/js-date0.htm#DC>
and
<http://www.merlyn.demo n.co.uk/js-date1.htm#diff>

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #12
JRS: In article <BD************ ***********@btc lick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang. javascript, Ian Sedwell
<ia*********@bt click.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.n et> 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.demo n.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.de mon.co.uk> wrote:
JRS: In article <BD************ ***********@btc lick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang. javascript, Ian Sedwell
<ia*********@bt click.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.n et> 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:t heHour<13?theHo ur: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.n et> 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.co m> 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******@BOGUS rochester.rr.co m> writes:
function showTheHour (theHour){
return theHour==0?12:t heHour<13?theHo ur: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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #18
JRS: In article <BD************ ***********@btc lick.com>, dated Mon, 6
Sep 2004 23:38:44, seen in news:comp.lang. javascript, Ian Sedwell
<ia*********@bt click.com> posted :
On 2004/09/06 20:56, in article Yl************* *@merlyn.demon. co.uk, "Dr
John Stockton" <sp**@merlyn.de mon.co.uk> wrote:
JRS: In article <BD624545.CDE5% ia*********@btc lick.com>, dated Mon, 6
Sep 2004 15:46:19, seen in news:comp.lang. javascript, Ian Sedwell
<ia*********@bt click.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)%1 2)+ [' am', ' pm'][+(H24>11)]

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

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

Similar topics

13
3933
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 validated using the TEXTn_validate event but I would like to stop the user before it gets that far.
4
10114
by: Manny Chohan | last post by:
Hi Can anyone tell me how i can calculate yesterday date using asp? Thanks manny
5
6738
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 i have extracted into a report writer. Look forward to hearing.. Cheers... SimonC
1
21369
by: bradleyc | last post by:
How would you calculate the difference between two dates?
2
3650
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: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm 02/01/2004 14:30pm 02/01/2004 19:50pm
5
7850
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, sEndDate) Dim iWorkDays
8
17579
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 an Age field (in years only) by comparing the date above to the Birth Date field. I have tried this and it doesn't work: =DateDiff("yyyy", , ) Displays the variance in years between the values of the Birthday1 and Birthday2 fields. (from Access...
7
54943
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 display. I tried "results=enddate-startdate" dates have month, day, year,and time of day. After this calculation I end up with no results. Any help would be appreciated. Walt
4
50193
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, '2006-09-09 14:00', '2006-09-09 16:00' ) ,( 2, '2006-09-10 09:00', '2006-09-10 17:00' ) ,( 3, '2006-09-11 13:30', '2006-09-11 14:45' )
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.