Need to convert the date using UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to convert the date using UNIX
# 1  
Old 11-25-2010
Java Need to convert the date using UNIX

Hi,

how to convert the date from DDMONYYY to DD/MM/YYYY
In my file I have a the date as 25OCT2008 but I want the O/P as 25/10/2008.

Can someone help me

Thanks in advance


Regards,
MKS
# 2  
Old 11-25-2010
one of the method to convert..
Code:
date -d "25OCT2008" "+%d/%m/%Y"

# 3  
Old 11-25-2010
Hi Michael,

below error is coming.
Code:
date -d "25oct2010" "+%d/%m/%Y"

date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

Many Thanks,
MKS

Last edited by Scott; 11-25-2010 at 08:49 PM.. Reason: Please use code tags
# 4  
Old 11-25-2010
Here... Script name Ddate

Code:
#!/usr/bin/bash
check="`echo $1 | cut -c3-5`"
day="`echo $1 | cut -c1-2`"
year="`echo $1 | cut -c6-9`" 
declare -a mon_
declare -a mon_d
mon_=("Jan" "Feb" "Oct" "Nov")
mon_d=("1" "2"  "10" "11") 
limit="`echo ${#mon_[@]}`"
for (( i=0; i<=${limit}; i++ ));
do
tmp="`echo ${mon_[$i]}`"
if [ "$tmp" == "$check" ];then
check_mon="`echo ${mon_d[$i]}`"
break
fi
done 
echo "$day/$check_mon/$year"



Usage:
Code:
./Ddate 22Oct2019
22/10/2019

Add more months Smilie

Last edited by mtomar; 11-25-2010 at 08:39 AM.. Reason: fixed code.
# 5  
Old 11-25-2010
Code:
echo 25JAN2008 |awk -F "" '
BEGIN{split("JAN FEB MAR APR MAI JUN JUL AUG SEP OCT NOV DEC",A," "); while (++i in A) M[A[i]]=i}
{sub($3$4$5,"/" sprintf("%02d",M[$3$4$5]) "/")}1'

25/01/2008

echo 25OCT2008 |awk -F "" '
BEGIN{split("JAN FEB MAR APR MAI JUN JUL AUG SEP OCT NOV DEC",A," "); while (++i in A) M[A[i]]=i}
{sub($3$4$5,"/" sprintf("%02d",M[$3$4$5]) "/")}1'
25/10/2008

if the month is not all capital letters:

Code:
echo 25Jan2008 |awk -F "" '
BEGIN{split("JAN FEB MAR APR MAI JUN JUL AUG SEP OCT NOV DEC",A," "); while (++i in A) M[A[i]]=i}
{$0=toupper($0);sub($3$4$5,"/" sprintf("%02d",M[$3$4$5]) "/")}1'

25/01/2008


Last edited by rdcwayx; 11-25-2010 at 08:27 PM..
# 6  
Old 11-26-2010
The solution suggested by "michaelrozar17" works. .. But with a small change:

odt=`date +%d/%m/%Y -d "25OCT2008"`

echo $odt # You will get 25/10/2008

Last edited by IND123; 11-26-2010 at 03:52 AM.. Reason: Removed the extra space before closing the back tick!
# 7  
Old 11-26-2010
If -d option is no avaliable:
Code:
 echo 25OCT2008|perl -n -e 'use Time::Piece;;/(\d\d)(\w{3})(\d{4})/;$t = Time::Piece->strptime("$1 $2 $3","%d %b %Y");print $t->strftime("%d\/%m\/%Y")."\n";'

This User Gave Thanks to Klashxx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert date in dd mm yyyy format to UNIX timestamp

Hello All, I have a date in DD/MM/YYYY format. I am trying to convert this into unix timestamp. I have tried following: date -d $mydate +%s where mydate = 23/12/2016 00:00:00 I am getting following error: date: extra operand `+%s' Try `date --help' for more information. ... (1 Reply)
Discussion started by: angshuman
1 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. UNIX for Dummies Questions & Answers

Unable to convert date into no. using date -d +%s syntax in ksh shell

hi friends, I m trying to write a script which compares to dates. for this i am converting dates into no using synatx as below v2=`date | awk '{print $2,$3,$4}'` v3=`date +%s -d "$v2"` this syntax is working in bash shell ,but fails in ksh shell. please suggest on this. (12 Replies)
Discussion started by: Jcpratap
12 Replies

4. AIX

convert a specific date to a unix timestamp

hello, i have an AIX5.3 machine and i am writing a script to display some processes. inside the script i want to get the time that the process starts and convert it to a unix timestamp. is there a command that i can use to do that? i search the web but all i found is long scripts and it does... (4 Replies)
Discussion started by: omonoiatis9
4 Replies

5. Shell Programming and Scripting

Need a unix script to convert date into Julian format in a text file

The 6th & 7th column of the text files represents date & time. I need this to be converted in julian format using command "date +%s -d <date>". I know the command, but dont know how to use it on the script 0 dbclstr-b IXT_Web Memphis_Prod_SQL_Full Memphis-Prod-SQL-Full-Application-Backup... (4 Replies)
Discussion started by: ajiwww
4 Replies

6. Shell Programming and Scripting

Convert date to Unix format

Dear Expert How to convert date in format of YYYY-MM-DD HH:MM:SS to unix format using a script or command if avaliable Example "2011-05-15 18:00:00" is converted to 1330970400 I tried to use option d in date command but no use, Im using solaris 10 Thanks a lot (12 Replies)
Discussion started by: yahyaaa
12 Replies

7. Shell Programming and Scripting

To convert a date(in string format) to unix timestamp

Hi All, I have a string like below. "Mar 31 2009" . I want to convert this to unix time . Also please let me know how to find the unix time for the above string minus one day. For Eg. if i have string "Mar 31 2009" i want to find the unix time stamp of "Mar 30 2009". Thanks in advance,... (11 Replies)
Discussion started by: girish.raos
11 Replies

8. Shell Programming and Scripting

Convert date to unix time

Hi, I need to convert a date in the format yyyy-mm-dd to unix seconds, shell script or perl would be ok since there is no hour/second, we can assume 12am every day thanks in advance funksen (4 Replies)
Discussion started by: funksen
4 Replies

9. Shell Programming and Scripting

convert unix date to readable format

Dear Experts, I need your help to convert a unix date and time format number in to readable format like dd/mm/yyyy . I have a text file of more than 10,000 records and it is like NAME DATE1 COUNTRY DATE2 ABD 1223580395699 USA 1223580395699... (3 Replies)
Discussion started by: shary
3 Replies

10. Shell Programming and Scripting

Convert String to Date Unix

Hi people, I need to convert a string eg 09/13/2008 to a valid unix date. (4 Replies)
Discussion started by: sameerspice
4 Replies
Login or Register to Ask a Question