Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Help with time offset in age script

1. ## Help with time offset in age script

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!

<script>

/*
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)
else
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")

</script>

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>
<script>
displayage(2002, 12, 13, "days", 0, "rounddown")
</script>
</b> or <br>

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

<b>
<script>
displayage(2002, 12, 13, "months", 1, "rounddown")
</script>
</b>

• 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.

• Gave that a shot, but I get a script error. ("Object doesn't support this property or method.") Any thoughts?

• Sorry, was thinking of a different system. The following code will do the same thing, it's just a bit longer.

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

• BINGO!