Operating System - Linux
1752275 Members
5424 Online
108786 Solutions
New Discussion юеВ

Re: Get last months month in alpha?

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Get last months month in alpha?

Hi,

Is there a simple way to get last month's month in alpha chars. For example, this month is "Feb", I need it to output last month "Jan"

Thanks,
S.
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Get last months month in alpha?

Here's one approach using caljd.sh:

#!/usr/bin/sh

caljd.sh $(caljd.sh) | read MO DUMMY YEAR
MON=$(caljd.sh -M -o $(caljd.sh -p 1 ${MO} 1 ${YEAR}))
echo "Month = ${MON}"

It works by determining the month day and year or the current month, then setting the day of the month to 1 and finding the Julian day of the previous day and then outputting the monthname. Change the "-o" in the last line to "-O" and the full month name will appear -- and full National Language Support works if LANG is set.

Here's caljd.sh; invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Get last months month in alpha?

Here's something that you could try though I would prefer to use caljd.sh

# let x=$(date +"%m")-1
# cal $(echo $x) $(date +%Y) | awk '{print substr($1,1,3);exit}'
Denver Osborn
Honored Contributor

Re: Get last months month in alpha?

THIS_MONTH=`date +%b`
[[ $THIS_MONTH = Jan ]] && LAST_MONTH=Dec
[[ $THIS_MONTH = Feb ]] && LAST_MONTH=Jan
[[ $THIS_MONTH = Mar ]] && LAST_MONTH=Feb
[[ $THIS_MONTH = Apr ]] && LAST_MONTH=Mar
..and so on..
echo Last month was $LAST_MONTH


-denver
spex
Honored Contributor

Re: Get last months month in alpha?

Hi Coolmar,

I had to do this one with pen and paper first...

$ echo "((($(date +%m)-1)+11)%12)+1"|bc

PCS
James R. Ferguson
Acclaimed Contributor

Re: Get last months month in alpha?

Hi:

Another:

# perl -le '$m=shift or die;@mon=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan);
$n = $m == 1 ? 11:$m-2;print @mon[$n]' month_number

Regards!

...JRF...
spex
Honored Contributor

Re: Get last months month in alpha?

Oops... I missed the alpha part with my response above...

set -A mo Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec && echo ${mo[$(echo "((($(date +%m)-1)+11)%12)"|bc)]}

PCS
Peter Nikitka
Honored Contributor

Re: Get last months month in alpha?

Hi,

a simple ksh array solution:

set -A mo NULL Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
print ${mon[$(date +%m)]}

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Get last months month in alpha?

Hi (again):

I agree with Peter N. :

If you unconditionally want the previous month without providing a month number as an arguement, then my Perl solution can be reduced to:

# perl -le '@mon=qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);$n=(localtime)[4];$n = $n == 0 ? 11:$n-1;print @mon[$n]'

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Get last months month in alpha?

Along the same lines:

$ perl -e "print substr(qq(DecJanFebMarAprMayJunJulAugSepOctNov),3*(localtime)[4],3)"
Jan
$

Without listing the month names in perl, but much more work :-)

Not useful here, but the principle may help

perl -e "use Time::Local; @t=localtime; if($t[4]) { $t[4]-- } else {$t[5]--;$t[4]=12} $t=timelocal(@t); $m=(split(" ",scalar l
ocaltime($t)))[1]; print $m"

Jan


use Time::Local; Find source
@t=localtime; Now as array ($sec,$min,$hour,$mday,$mon,$year);
if($t[4]); Anything but Jan?
{ $t[4]-- } Just decrement the month
else {
$t[5]--;$t[4]=12} Month = 12
$t=timelocal(@t); Time in seconds
$m=(split(" ", 2nd word
scalar localtime($t)))[1]; Time as text
print $m" Finally!

Hein