Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Get last months month in alpha?

SOLVED
Go to Solution
Esteemed Contributor 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.
1 ACCEPTED SOLUTIONS
Acclaimed Contributor Acclaimed Contributor

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.
13 REPLIES
Acclaimed Contributor Acclaimed Contributor

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.
Honored Contributor 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}'
Honored Contributor 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
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
Acclaimed Contributor 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...
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
Honored Contributor 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"
Acclaimed Contributor 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...
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
Acclaimed Contributor Acclaimed Contributor

Re: Get last months month in alpha?

I would prefer a more portable version that leverages "locale LC_TIME" for NLS but that's just me. With caljd.sh you get that for free.
If it ain't broke, I can fix that.
Acclaimed Contributor Acclaimed Contributor

Re: Get last months month in alpha?

>Clay: I would prefer a more portable version that leverages "locale LC_TIME"

You could take the output from "locale abday", strip the ";" and double quotes and fill your array.
$ set -A WEEKDAY $(locale abday | sed -e 's/[;"]/ /g')
$ echo ${WEEKDAY[@]}
Sun Mon Tue Wed Thu Fri Sat
Acclaimed Contributor Acclaimed Contributor

Re: Get last months month in alpha?

Oops, Coolmar wanted months:
$ set -A MONTHS $(locale abmon | sed -e 's/[;"]/ /g')
$ echo ${MONTHS[*]}
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

And to print out last month:
echo ${MONTHS[($(date +%m)+10)%12]}
Highlighted
Esteemed Contributor Esteemed Contributor

Re: Get last months month in alpha?

Thanks everyone for all the suggestions!