calculate the number of days left in a month


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calculate the number of days left in a month
# 1  
Old 08-11-2008
calculate the number of days left in a month

does any one have any ideas how i would go about calculating the number of days left in the month from a bash script ?. I want to do some operations on a csv file according to the result
# 2  
Old 08-11-2008
usage: sh getdays.sh 08 2008

Code:
if [ -n "$1" ]
 then
  dim_m=$1
  dim_y=$2
 else
  eval `date "+dim_m=%m dim_y=%Y"`
 fi
 _DAYS_IN_MONTH=`cal $dim_m $dim_y | egrep -v '[A-Za-z]' | wc -w`

echo $_DAYS_IN_MONTH


EDIT: This only gets the number of total days in month. :P

Last edited by Ikon; 08-11-2008 at 05:59 PM..
# 3  
Old 08-11-2008
@ikon thanks for that so to calculate the number of days left in the month i used (added)

Quote:
#!/bin/bash
if [ -n "$1" ]
then
dim_m=$1
dim_y=$2
else
eval `date "+dim_m=%m dim_y=%Y"`
fi
_DAYS_IN_MONTH=`cal $dim_m $dim_y | egrep -v '[A-Za-z]' | wc -w`

echo $_DAYS_IN_MONTH

_DATE_TODAY=`date +%d`

echo $_DATE_TODAY

let SUBTRACTION=$_DAYS_IN_MONTH-$_DATE_TODAY
echo $SUBTRACTION
which seem to work great thanks alot

Kind regards dunryc
# 4  
Old 08-11-2008
simple!!!!!!!

usage scriptname month(MM) year(YYYY)

let no_of_days_left_in_month=`cal $1 $2|sed '/^$/d'|tail -1|awk '{print $NF}'`-`date "+%d"`
echo "$no_of_days_left_in_month"

Last edited by vidyadhar85; 08-11-2008 at 07:29 PM..
# 5  
Old 08-11-2008
Quote:
Originally Posted by vidyadhar85
usage scriptname month(MM) year(YYYY)

let no_of_days_left_month=`cal $1 $2|sed '/^$/d'|tail -1|awk '{print $NF}'`-`date "+%d"`
echo "$no_of_left_in_month"
I get nothing...

Code:
# sh dom.sh 08 2008
 
#

# 6  
Old 08-11-2008
fnsonlq0-/home/fnsonlq0/vidya_perl>cat calculate
let no_of_days_left_in_month=`cal $1 $2|sed '/^$/d'|tail -1|awk '{print $NF}'`-`date "+%d"`
echo "\n$no_of_days_left_in_month\n"

fnsonlq0-/home/fnsonlq0/vidya_perl>calculate 08 2008
19
fnsonlq0-/home/fnsonlq0/vidya_perl>

I AM NOT GETTING ANY PROBLEM!!!!!!!
# 7  
Old 08-11-2008
I see you changed the string:

Code:
let no_of_days_left_in_month=`cal $1 $2|sed '/^$/d'|tail -1|awk '{print $NF}'`-`date "+%d"`
echo "\n$no_of_days_left_in_month\n"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of days in current month

I have a homework assignment: ---------------------------------------- "Display" the number of days in the current month. For example: September 1996 has 30 days ---------------------------------------- I am trying to just display the head of cal to start the sentence. eg. cal | head ... (1 Reply)
Discussion started by: eaafuddy
1 Replies

2. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

3. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

4. Shell Programming and Scripting

How to Find number of days in a month in mmddyyyy format?

Hi Guru's, I am working on a shell script from past a month and unable to get rid of automating while working with dates,here's what i have. inital_date=11012011 final_date=11302011 expected_output= has to be in below format PFB 11012011 11022011 11032011 * * * 11102011 * *... (9 Replies)
Discussion started by: Gaurav198
9 Replies

5. Shell Programming and Scripting

display number of days in current month

hi all searched google and here, cant find and am begining to suspect there is no options for this. shell = born with either the date or cal command I need to display the number of days in current month. can anyone point me in the right direction? (10 Replies)
Discussion started by: rontopia
10 Replies

6. Programming

Number of days in month from certain parameters, c programming request.

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (2 Replies)
Discussion started by: modn3
2 Replies

7. Shell Programming and Scripting

Number of days in month from certain parameters

Hi, I have an issue in date processing, the issue is I have a month as an int ( 1 - 12 ), the weekday as int ( 0 - 6 , 0 = Sunday), and the week day in month as int ( 0 - 5, 5 = last ex: first sunday, last monday, third tuesday ... ), now from those three parameters is there a possible way to... (5 Replies)
Discussion started by: modn3
5 Replies

8. Shell Programming and Scripting

Number of days in the previous month

Hi all. I am scripting in a POSIX shell on HPUX. I am running a script that needs to determine the number of days in a month. I found this on the forum and it works great: X=`cal $(date +%m) $(date +%Y) | grep -v '' | wc -w` The issue is that I am running the script on the 7th day of... (11 Replies)
Discussion started by: lyoncc
11 Replies

9. Shell Programming and Scripting

Assigning number of days in the month to a variable

I am writing a script that requires the number of days in any given month. In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc... (3 Replies)
Discussion started by: skaptakalian
3 Replies

10. Shell Programming and Scripting

Calc number of days in a month

Looking for some help on capturing the number of days in a month to set as a loop counter. Any ideas, please let me know. (3 Replies)
Discussion started by: flounder
3 Replies
Login or Register to Ask a Question