Visit Jeremy's Blog.
 LinuxQuestions.org Seeking help with date calculations in bash (on MacOS-X)
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 09-26-2013, 08:25 AM #16 Firerat Senior Member   Registered: Oct 2008 Distribution: Debian sid Posts: 2,001 Rep: Save/Restore your IFS.. Code: ```OrigIFS="\$IFS" IFS=- ..... ..... IFS="\$OrigIFS"``` As it might interfere with things you do later in your script
 09-26-2013, 08:29 AM #17 sparky90ful Member   Registered: Sep 2013 Posts: 30 Original Poster Rep: SO i am here Code: ```string=\$1 IFS=- set \$string year=\$3 mounth=\$2 day=\$1 an=`date +%Y` anne=`expr \$an - \$year` echo "Age : \$anne ans"``` how i can find how many days it rest until his next birthday?
09-26-2013, 08:38 AM   #18
TenTenths
Senior Member

Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,930

Rep:
Quote:
 Originally Posted by sparky90ful SO i am here Code: ```string=\$1 IFS=- set \$string year=\$3 mounth=\$2 day=\$1 an=`date +%Y` anne=`expr \$an - \$year` echo "Age : \$anne ans"``` how i can find how many days it rest until his next birthday?
Looking at your code, your calculation for the age is based on a wrong assumption.

You can't just subtract years to get someones age.

For example, my birthday is 16th October, so until that date I'm actually a year younger than a simple subtraction.

Have another think around what colucix suggested, "seconds since epoch".

 09-26-2013, 08:40 AM #19 Firerat Senior Member   Registered: Oct 2008 Distribution: Debian sid Posts: 2,001 Rep: add a year find difference from today ( in seconds ) do maths to get from seconds to days for fun, instead of exiting count down the remaining seconds read about printf http://wiki.bash-hackers.org/commands/builtin/printf ( in all honesty,. I skimmed that.. not certain how good a read it is )
 09-26-2013, 08:44 AM #20 sparky90ful Member   Registered: Sep 2013 Posts: 30 Original Poster Rep: until now i have this Code: ```string=\$1 IFS=- set \$string year=\$3 mounth=\$2 day=\$1 an=`date +%Y` luna=`date +%m` anne=`expr \$an - \$year` if [[ \$luna < \$mounth ]] then anne=`expr \$an - \$year + 1` fi echo "Age : \$anne ans"``` 9 now i have to find how many days it is until his next birthday how do i do that (1 year have 365 days not much not less) ps:that is good for ages ?? Last edited by sparky90ful; 09-26-2013 at 08:45 AM.
 09-26-2013, 08:49 AM #21 Firerat Senior Member   Registered: Oct 2008 Distribution: Debian sid Posts: 2,001 Rep: it got my age wrong
09-26-2013, 08:50 AM   #22
TenTenths
Senior Member

Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,930

Rep:
Quote:
 Originally Posted by sparky90ful ps:that is good for ages ??
Still doesn't take into account the day of the month. Run it for a birthday of yesterday in 2000 and of tomorrow in 2000 and you'll see.

09-26-2013, 08:50 AM   #23
sparky90ful
Member

Registered: Sep 2013
Posts: 30

Original Poster
Rep:
Quote:
 Originally Posted by Firerat it got my age wrong
why where is the error for my byrthday date is god

09-26-2013, 08:56 AM   #24
TenTenths
Senior Member

Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,930

Rep:
Quote:
 Originally Posted by Firerat it got my age wrong
Mine too.

09-26-2013, 08:57 AM   #25
sparky90ful
Member

Registered: Sep 2013
Posts: 30

Original Poster
Rep:
Quote:
 Originally Posted by TenTenths Mine too.
wrong with 1 in plus or with one in minus

Code:
```string=\$1
IFS=-
set \$string
year=\$3
mounth=\$2
day=\$1
an=`date +%Y`
luna=`date +%m`
anne=`expr \$an - \$year`
if [[ \$luna < \$mounth ]]
then
anne=`expr \$an - \$year - 1`
fi
echo "Age : \$anne ans"```
now is good?

Last edited by sparky90ful; 09-26-2013 at 08:59 AM.

 09-26-2013, 08:58 AM #26 Firerat Senior Member   Registered: Oct 2008 Distribution: Debian sid Posts: 2,001 Rep: dates are awful for calulations it has been hinted at a few times now you need to use the date command man date Code: ``` %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab``` and .. 1 - -1 = 2
 09-26-2013, 08:59 AM #27 Pap Member   Registered: May 2011 Distribution: Salix 14.1 GNU/Linux, 64-bit Posts: 70 Rep: The following script will compute and print the time elapsed since the date you give as an argument and present time: #!/bin/bash Code: ```function elapsed_time { local user_date=\$(date --date=\$1 +%s) local now=\$(date +%s) local dt=\$((now-user_date)) local dyears=\$((dt / 31536000)) if [ \$dyears -gt 0 ];then echo -n "\$dyears years, ";fi dt=\$((dt % 31536000)) local dmonths=\$((dt / 2592000)) if [ \$dmonths -gt 0 ];then echo -n "\$dmonths months, ";fi dt=\$((dt % 2592000)) local ddays=\$((dt / 86400)) if [ \$ddays -gt 0 ];then echo -n "\$ddays days, ";fi dt=\$((dt % 86400)) local dhours=\$((dt / 3600)) if [ \$dhours -gt 0 ];then echo -n "\$dhours hours, ";fi dt=\$((dt % 3600)) local dminutes=\$((dt / 60)) if [ \$dminutes -gt 0 ];then echo -n "\$dminutes minutes, ";fi dseconds=\$((dt % 60)) echo \$dseconds seconds. } echo -n "Elapsed time: ";elapsed_time \$1``` Save it as, say, "elapsed_time", make it executable, and provide the date as in the example below: Code: ```./time_elapsed 12-May-1993 Elapsed time: 20 years, 4 months, 22 days, 17 hours, 3 minutes, 8 seconds.``` As you can see, the script expects a date in the dd-mmm-yyyy format, so it calculates time elapsed till the beginning of the specified day; for example, time elapsed from 26-Sep-2013, 00:00, till the time I am typing this is: Code: ```./time_elapsed Sep-26-2013 Elapsed time: 17 hours, 12 minutes, 40 seconds.``` With some modifications, the script can do much more things, such as automatically calculating the time till next Christmas, or the time till your next birthday, but I'll leave that as an exercise to the reader Last edited by Pap; 09-26-2013 at 09:16 AM.
09-26-2013, 09:08 AM   #28
Habitual
LQ Veteran

Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep:
Quote:
 Originally Posted by sparky90ful is what i must create file.sh is the script writed by me and when user is run it with some parameter i must have the result for nomather which will be the parameter for now i have this Code: ```string=\$1 IFS=- set \$string year=\$3 mounth=\$2 day=\$1``` y must compare with the actual date to find the age
Date formats are different for different regions of the planet.
Is that expected input always supposed to be MM-DD-YYYY? (05-12-1993)
That may be taken as Dec 12, 1993 in Europe...I think.(I'm an American m-d-y is the 'norm' here.)

You may wish to advise the user of the expected input format (echo "Please use MM-DD-YYYY as the format")
Search MINPARAMS here...

some hints for "date math"
Code:
```date +"%m-%d-%Y"
date +"%m-%d-%Y" --date="+6 months +29 days"
date +"%m-%d-%Y" --date="+365 days"
date +"%m-%d-%Y" --date="+364 days"
date +"%m-%d-%Y" --date="+11 months 26 days"```
Very flexible.

That should keep you from scratching your eyes out.

Good Luck and let us know...

and what Pap posted.

Last edited by Habitual; 09-26-2013 at 09:09 AM.

09-26-2013, 09:11 AM   #29
Firerat
Senior Member

Registered: Oct 2008
Distribution: Debian sid
Posts: 2,001

Rep:
Quote:
 Originally Posted by sparky90ful wrong with 1 in plus or with one in minus now is good?
afraid not
try these two dates

28-09-1900
28-10-1900

they should give the same answer ( today is 26-09-2013 dd-mm-yyyy )
but they don't

09-26-2013, 09:12 AM   #30
sparky90ful
Member

Registered: Sep 2013
Posts: 30

Original Poster
Rep:
Quote:
 Originally Posted by Pap The following script will compute and print the time elapsed since the date you give as an argument and present time: #!/bin/bash Code: ```function elapsed_time { local user_date=\$(date --date=\$1 +%s) local now=\$(date +%s) local dt=\$((now-user_date)) local dyears=\$((dt / 31536000)) if [ \$dyears -gt 0 ];then echo -n "\$dyears years, ";fi dt=\$((dt % 31536000)) local dmonths=\$((dt / 2592000)) if [ \$dmonths -gt 0 ];then echo -n "\$dmonths months, ";fi dt=\$((dt % 2592000)) local ddays=\$((dt / 86400)) if [ \$ddays -gt 0 ];then echo -n "\$ddays days, ";fi dt=\$((dt % 86400)) local dhours=\$((dt / 3600)) if [ \$dhours -gt 0 ];then echo -n "\$dhours hours, ";fi dt=\$((dt % 3600)) local dminutes=\$((dt / 60)) if [ \$dminutes -gt 0 ];then echo -n "\$dminutes minutes, ";fi dseconds=\$((dt % 60)) echo \$dseconds seconds. } echo -n "Elapsed time: ";elapsed_time \$1``` Save it as, say, "elapsed_time", make it executable, and provide the date as in the example below: Code: ```pap@debian:/pap/scripts\$ ./time_elapsed 12-May-1993 Elapsed time: 20 years, 4 months, 22 days, 17 hours, 3 minutes, 8 seconds.``` With some modifications, the above script can also be used to compute the time till your next birthday, but I'll leave that as an exercise to the user

it gives me this
Code:
```01-r3p05:ex_4 florea_g\$ ./incercare.sh 15-May-1990
Elapsed time: date: illegal option -- -
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
43 years, 9 months, 9 days, 14 hours, 10 minutes, 50 seconds.
301-r3p05:ex_4 florea_g\$```

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post taylor_ma Programming 9 05-03-2012 08:27 PM [SOLVED] Date Calculations using script!! anishkumarv Linux - Newbie 9 09-24-2011 03:36 PM w1k0 Programming 5 06-07-2009 08:10 PM ukursat Linux - Newbie 1 12-05-2008 10:01 PM Crashman Programming 4 07-03-2004 10:15 AM

LinuxQuestions.org

All times are GMT -5. The time now is 06:53 AM.