Convert month(06) to String(Jun)


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Convert month(06) to String(Jun)
# 1  
Old 05-29-2013
Convert month(06) to String(Jun)

Hi All,

From the below code i can see the value in M=05 and +1 added i.e 6.
i am calling those 2 variables and the output is giving as 01-6-2013. But the required output should be 01-Jun-2013. Please let me know how can i get this format.

Code:
M=$(date +%m)
Y=$(date +%Y)
if [ $M == 12 ] ; then 
        M=01
        Y=$(($Y+1))
else
        M=$(($M + 1))
fi;

Thanks in Advance..
# 2  
Old 05-29-2013
If you're using a recent bash or ksh, try:
Code:
m=( "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" )
M=$(date +%m)
Y=$(date +%Y)
if [ $M == 12 ]
then    M=0
        Y=$(($Y+1))
fi
printf "01-%s-%d\n" ${m[$M]} $Y

# 3  
Old 05-29-2013
Two comments on Don Cragun's proposal:
Arrays in both shells mentioned start at index 0; so you should use sth like $M-1 as an index (you then should not set M=0, obviously).
Although quoting is usually encouraged, assigning an array of constants like above, you can do without.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 05-29-2013
Quote:
Originally Posted by RudiC
Two comments on Don Cragun's proposal:
Arrays in both shells mentioned start at index 0; so you should use sth like $M-1 as an index (you then should not set M=0, obviously).
Although quoting is usually encouraged, assigning an array of constants like above, you can do without.
Yes, I realized both of these after I sent my lost message in this thread. I used more of the OP's original code than was needed to get the job done. Another way to do it (without subtraction) is:
Code:
m=(0 Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan)
M=$(date +%m)
Y=$(date +%Y)
[ $M == 12 ] && Y=$((Y+1))
printf "01-%s-%d\n" ${m[$M]} $Y

# 5  
Old 05-29-2013
Try
Code:
months=(0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec )
Y=$((Y + (M / 12)))
M=$((M % 12 + 1))
printf "01-%s-%d\n" ${months[$M]} $Y

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace integer string in a variable based on month?

Hi Folks - Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64 I have a process that determines the start and end load periods for an Oracle data load process. The variables used are as follows follows: They are populated like such: However, the load requires the month to be the... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

Remove new line and convert Month to Decimal

# Sample input common-name www.test.com.au expiration Dec 21 01:00:31 2017 GMT common-name www.test1.com.au expiration Jan 19 04:41:03 2018 GMT # Desired Output # Field 1: Domain name # Field 2: Date/time converted to Austraian format DD/MM/YYYY and on the same line as Domain Name. #... (7 Replies)
Discussion started by: thangbom
7 Replies

3. Shell Programming and Scripting

Convert From Month Number to Month Name

Hi, I have a script that accepts an input date from the user in yyyy-mm-dd format. I need to get the mm-dd part and convert it to month name. example: 2011-11-15 I want that to become "Nov 15" I don't have the GNU date, I am using an AIX os. Thanks. (1 Reply)
Discussion started by: erin00
1 Replies

4. Shell Programming and Scripting

How to convert date format such as 7/18/2015 to the number of month

How to convert date format such as 7/18/2015 to the number of month from requesting date 'date' in sh scripting ? Let say I have output in my log.txt -> 7/18/2015. How I convert it to the full number of month starting from 'date' till 7/18/2015 in shell scripting ? Thanks in advance. (1 Reply)
Discussion started by: amerabest
1 Replies

5. Shell Programming and Scripting

gawk convert 2012-Jun-13 to 2012-06-13

I have a value in a file i am processing that has a date like "2012-Jun-13" how can I convert a date like that 2012-06-13? Am I stuck building an array of three digit months and corresponding numbers and running through the logic of figuring out the number?? or can I convert this with... (1 Reply)
Discussion started by: trey85stang
1 Replies

6. AIX

Convert unix timestamp to year month day format ?

Hello, How do I convert unix timestamp value to 'normal' date format - to get year month and day values ? Looks like it's easy to do using GNU date (linux systems). But how do I do tthis on AIX ? I don't want to write C program, any ways to do that using unix shells ? thanks (1 Reply)
Discussion started by: vilius
1 Replies

7. Shell Programming and Scripting

Convert into month name

Hi, If I am having month like 2/3/4 then how can I convert it into month name like Feb/Mar/Apr.... Is there any defined function in Perl ?? (2 Replies)
Discussion started by: darshakraut
2 Replies

8. Shell Programming and Scripting

How to convert DDMMYYYY to DD MONTH YYYY in Unix

Hi I am having date as a string in DDMMYYYY format(07082008) in a variable say cdate. I want to Convert it into DD Month YYYY format(7 August 2008). Could someone help. Thanks in Advance. (2 Replies)
Discussion started by: rspk_praveen
2 Replies

9. Shell Programming and Scripting

How to print current month - 1 in string format

I need to get the current month -1 in string format, like Feb-2006. I am using k shell. Any help is appreciated. (3 Replies)
Discussion started by: mogli4
3 Replies

10. UNIX for Dummies Questions & Answers

To convert the numeric month into alphabetic

hi , This function actually gives me the last month but as a numeric value what should i do to get it as suppose :Jul date '+%b %Y' | { read MONTH YEAR MONTH=`expr "$MONTH" - 1` echo $MONTH } Thanks. Rooh (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question