Register
It is currently Sat Dec 20, 2014 9:28 am

need help with a formula


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri Mar 04, 2011 3:31 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
i'm trying to calculate how many days someone has been alive in this century. For some reason I can't wrap my head around the proper formula, can someone point me in the right direction? I know to get a rough estimate its just something like

Code:
currentyear=2011
century=$(((currentyear-2000)*365))


but thats not very accurate.. anyone have any suggestions?


Top
 Profile  
 PostPosted: Fri Mar 04, 2011 3:38 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
does this seem accurate?

Code:
dateyear=2011
monthsalive=4
bdayday=24
century=$(((dateyear-2000)*365+(monthsalive*30)+(bdayday)))


i just used the variables as examples, the variables are actually going to be taken from user input


Top
 Profile  
 PostPosted: Fri Mar 04, 2011 4:17 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
There's a lot of room for error there in that not every year has 365 days, and not every month has 30 days!

You might get more accurate results if you convert your dates to epoch time, do the math with those numbers, and then convert the result back to something easier to read. Here's a starting point:

Code:
# Current time/date in seconds...
date +%s                                                                                                             
1299275974

# Midnight on Jan 1st, 2000
date -d "01/01/2000" +%s
946706400

# Then we can subtract those numbers, and convert
# the result to days (if that's what you're looking for)
current=$(date +%s)
century=$(date -d "01/01/2000 00:00:00" +%s)
days_this_century=$((($current-$century)/60/60/24))
echo $days_this_century


Disclaimer: not a lot of thought went into this demonstration, so I can vouch for the accuracy, but I think it's close, and should get you going in the right direction!

Hope this helps!
-Jeo


Top
 Profile YIM  
 PostPosted: Fri Mar 04, 2011 4:22 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
To get the epoch time based on user input:

Code:
month=06
day=01
year=1976
date -d "${month}/${day}/${year}" +%s


Top
 Profile YIM  
 PostPosted: Fri Mar 04, 2011 4:28 pm   

Joined: Mon Feb 28, 2011 12:36 pm
Posts: 8
it does help a lot, so if a person was born say on feb 22 2004, i'd just need to subtract that from the total amount of days in this century correct?


Top
 Profile  
 PostPosted: Sun Mar 06, 2011 12:23 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Sounds right to me!


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP