Converting Date from YYYYMMDD to DD-MON-YYYY


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Converting Date from YYYYMMDD to DD-MON-YYYY
# 1  
Old 12-15-2011
Converting Date from YYYYMMDD to DD-MON-YYYY

Hi , I need to convert date from YYYYMMDD to DD-MON-YYYY

e.g
20111214 to 14-Dec-2011

Please help.
# 2  
Old 12-15-2011
# 3  
Old 12-15-2011
here is your code.. :-)

Code:
date=20111214
len=${#date}
echo $len
dat=${date:6:8}
yr=${date:0:4}
mon=${date:4:6}
mon=${mon:0:2}
var1="jan"
var2="feb"
var3="mar"
var4="apr"
var5="may"
var6="jun"
var7="jul"
var8="aug"
var9="sep"
var10="oct"
var11="nov"
var12="dec"
val=$( eval eval echo \$var$mon )
echo "$dat-$val-$yr"

# 4  
Old 12-15-2011
Code:
$ date --date="20111214" +"%d-%b-%Y"
14-Dec-2011

or

Code:
$ echo "20111214" > my_dates_file.txt
$ date -f my_dates_file.txt +"%d-%b-%Y"
14-Dec-2011


Last edited by ni2; 12-15-2011 at 03:56 AM.. Reason: Added code tags and corrected initial post.
# 5  
Old 12-15-2011
@vivek ... Thanks but its not working . It is giving bad substitution error.
I am a novice to scripting . Also I am using as Sun Solaris unix machine

---------- Post updated at 03:01 AM ---------- Previous update was at 02:59 AM ----------

@ni2 date command --date and -f options are not available on sun solaris machine
# 6  
Old 12-15-2011
@ady_koolz: my code is tested it works fine in linux.. i donno about solaris... but anyway try this once.. comment this line
#mon=${mon:0:2}

and try.. hope it works...

---------- Post updated at 01:50 PM ---------- Previous update was at 01:46 PM ----------

@ni2: your code is best dude... its damn simple :-)
# 7  
Old 12-15-2011
@vivek its still not working on solaris....anywayz thanx...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Date format change from mm/dd/yyyy to yyyymmdd in comma seperate line in perl

Hi All, I have line ,A,FDRM0002,12/21/2017,,0.961751583,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, it contains date in mm/dd/yyyy format i want to change this to yyyymmdd format using perl. Use code tags, thanks. (8 Replies)
Discussion started by: vishal0746
8 Replies

2. Shell Programming and Scripting

Date conversion help from dd/mm/yyyy to dd/Mon/yyyy i.e. 28/10/2012 to 28/Oct/2012

Hi I have a problem with Date format in my code. 1st I am trying to convert today's date to yesterday's using YESTERDAY3=`perl -e '@y=localtime(time()-86400); printf "%04d/%02d/%02d",$y+1900,$y+1,$y;$y;'` And once it is done I am trying to using the yesterday date in a grep command to... (3 Replies)
Discussion started by: nithinankam
3 Replies

3. Shell Programming and Scripting

Converting date DD MM YYYY to DD MON YYYY

Hello, I am writing a script that parses different logs and produces one. In the source files, the date is in DD MM YYYY HH24:MI:SS format. In the output, it should be in DD MON YYY HH24:MI:SS (ie 25 Jan 2010 16:10:10) To extract the dates, I am using shell substrings, i.e.: read line ... (4 Replies)
Discussion started by: Adamm
4 Replies

4. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

5. Shell Programming and Scripting

PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All, I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal. The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy. Any ideas to do this? Thanks a lot for the... (9 Replies)
Discussion started by: guruparan18
9 Replies

6. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

7. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

8. Shell Programming and Scripting

converting the date field from dd/mm/yyyy to yyyy/mm/dd

How to convert the date field from dd/mm/yyyy to yyyy/mm/dd in unix my script will generate text file which have two fields one is date and another is name of the server for example this is sample date which I have to sort based on older to newer date the problem is when I found out sort will... (4 Replies)
Discussion started by: pareshan
4 Replies

9. Shell Programming and Scripting

Change Date from dd-mmm-yyyy to mm/dd/yyyy

I want to change a date from format dd-mmm-yyyy to mm/dd/yyyy. Is there a way to do this with sed or do you have to write a case statement to convert JAN to 01? Thanks (9 Replies)
Discussion started by: stringzz
9 Replies

10. UNIX for Dummies Questions & Answers

how to convert the string YYYYMMDD into YYYY.MM.DD

how to convert the string YYYYMMDD into YYYY.MM.DD Please advice (1 Reply)
Discussion started by: spatra
1 Replies
Login or Register to Ask a Question