473,782 Members | 2,498 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 4417
In article <U0************ *****@twister.n yroc.rr.com>, Mick White
<mw******@BOGUS rochester.rr.co m> 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******@BOGUS rochester.rr.co m> 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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #22
JRS: In article <u0**********@h otpop.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******@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 :)


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.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 #23
JRS: In article <gd************ *************** *****@4ax.com>, dated
Tue, 7 Sep 2004 03:53:05, seen in news:comp.lang. javascript, Otter
<Ot***@nospam.n et> 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.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 #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******@BOGUS rochester.rr.co m> 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.demo n.co.uk/js-date9.htm#1224> .

--
© 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 #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******@BOGUS rochester.rr.co m> 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.demo n.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
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
9641
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
10313
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...
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
8968
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
7494
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
6735
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.