Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, January 7, 2016

How to use bash to get the last day of each month for the current year without using if else or switch or while loop?

How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


As we know that each year have the following max day in each month as follows:

Jan - 31 days  Feb - 28 days / 29 days (leap year)  Mar - 31 days  Apr - 30 days  May - 31 days  Jun - 30 days  Jul - 31 days  Aug - 31 days  Sep - 30 days  Oct - 31 days  Nov - 30 days  Dec - 31 days  

How to I get bash to return the value (last day of each month) for the current year without using if else or switch or while loop?

Answer by amdn for How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


Assuming you allow "for", then the following in bash

for m in {1..12}; do      echo $(date -d $m/1/1 +%b) - $(date -d "$(($m%12+1))/1 - 1 days" +%d) days  done  

produces this

 Jan - 31 days   Feb - 29 days   Mar - 31 days   Apr - 30 days   May - 31 days   Jun - 30 days   Jul - 31 days   Aug - 31 days   Sep - 30 days   Oct - 31 days   Nov - 30 days   Dec - 31 days  

Note: I removed the need for cal

For those that enjoy trivia:

Number months from 1 to 12 and look at the binary representation in four  bits {b3,b2,b1,b0}.  A month has 31 days if and only if b3 differs from b0.  All other months have 30 days except for February.  

So with the exception of February this works:

for m in {1..12}; do      echo $(date -d $m/1/1 +%b) - $((30+($m>>3^$m&1))) days  done  

Result:

Jan - 31 days  Feb - 30 days (wrong)  Mar - 31 days  Apr - 30 days  May - 31 days  Jun - 30 days  Jul - 31 days  Aug - 31 days  Sep - 30 days  Oct - 31 days  Nov - 30 days  Dec - 31 days  

Answer by clyfish for How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


cat <

Answer by Steve for How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


Contents of script.sh:

#!/bin/bash  begin="-$(date +'%-m') + 2"  end="10+$begin"    for ((i=$begin; i<=$end; i++)); do      echo $(date -d "$i month -$(date +%d) days" | awk '{ printf "%s - %s days", $2, $3 }')  done  

Results:

Jan - 31 days  Feb - 29 days  Mar - 31 days  Apr - 30 days  May - 31 days  Jun - 30 days  Jul - 31 days  Aug - 31 days  Sep - 30 days  Oct - 31 days  Nov - 30 days  

Answer by glenn jackman for How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


my take:

for m in {1..12}; do    date -d "$m/1 + 1 month - 1 day" "+%b - %d days";   done  

Answer by Josiah DeWitt for How to use bash to get the last day of each month for the current year without using if else or switch or while loop?


returns the number of days in the month compensating for February changes without looping or using an if statement and only calls date once.

function daysin()  {     s="303232332323"                                        # leap year     date -d "2/29/$2" > /dev/null 2>&1 && s="313232332323"  # normal     echo $[ ${s:$[$1-1]:1} + 28 ]  }    daysin $1 $2                                               #daysin [1-12] [YYYY]  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.