View Full Version : Help with time offset in age script

06-04-2003, 08:18 PM
Hey there... newbie here needing help with an age script.

I modified a script to show my newborn son's age in days, weeks and months. The modification is ugly, but it works. (Note that the "year" calculation is actually divided by 52 to get me weeks.)

However, it is pulling the time from my host's server, located in Hong Kong. I'd like to make it reflective of Pacific Time so that the days and weeks are more accurate.

If I've done my math right, I'm looking at adding a -15 hour offset. However, I don't know where to put it in the script, nor do I know if I need to do it in milliseconds or not.

(And if I wanted to get really crazy, it would be cool to put in an offeset that would reflect his actual time of birth (10:08 a.m.), which, if my math is right, would be a +10 hour offset from midnight of any given day.)

Any help would be great. Thanks!


Ultimate Age calculator script- By JavaScript Kit (http://www.javascriptkit.com)
Over 200+ free scripts here!
Credit must stay intact for use

var one_day=1000*60*60*24
var one_month=1000*60*60*24*30
var one_year=1000*60*60*24*30*12/52

function displayage(yr, mon, day, unit, decimal, round){
today=new Date()
var pastdate=new Date(yr, mon-1, day)

var countunit=unit
var decimals=decimal
var rounding=round

finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
decimals=(decimals<=0)? 1 : decimals*10

if (rounding=="rounddown")
document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)

//Sample usage
//displayage (year, month, day, unit, decimals, rounding)
//Unit can be "years", "months", or "days"
//Decimals specifies demical places to round to (ie: 2)
//Rounding can be "roundup" or "rounddown"

//displayage(2002, 12, 13, "years", 0, "rounddown")


Tyler's age today:</b></font><p align="center" style="line-height: 100%; word-spacing: 0; margin-top: 0; margin-bottom: 0"> <font face="Arial"> <br><b>
displayage(2002, 12, 13, "days", 0, "rounddown")
</b> or <br>

displayage(2002, 12, 13, "weeks", 0, "rounddown")
</b> or <br>

displayage(2002, 12, 13, "months", 1, "rounddown")

Roy Sinclair
06-04-2003, 09:12 PM
This should work (add the part in red to the line with the part in black):
today=new Date() + (-15 * 60 * 60 * 1000)

The "-15" above reflects the hours to offset so you can adjust it as you find necessary. You could also multiply all that out and use a single number of milliseconds but spelling it out makes it simpler to understand what you're adjusting.

06-04-2003, 09:23 PM
Gave that a shot, but I get a script error. ("Object doesn't support this property or method.") Any thoughts?

Roy Sinclair
06-04-2003, 09:40 PM
Sorry, was thinking of a different system. The following code will do the same thing, it's just a bit longer.

today=new Date()
var tmpMilliseconds = today.getMilliseconds()
tmpMilliseconds += -15 * 60 * 60 * 1000

06-04-2003, 09:49 PM
Thanks for your help!