Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have one problem with my date format. I want to change from one format to the other and vice versa. My date formats are

Format 1

YYYY-MM-DD

Format 2

MM/DD/YYYY

I want to change format 1 to format 2 and format 2 to format 1 .

share|improve this question
    
o be more clear, I have the file file1 name: date 2000-01-01 2000-01-02 2000-01-03 2000-01-04 .. Required output is 01/01.2000 01/02/2000 01/03/2000 01/02/2000 and vice versal –  Abraham Sep 5 '13 at 19:11
    
Forward-slashes are one of the only two characters that cannot be in a *nix filename... –  evilsoup Sep 5 '13 at 20:41

3 Answers 3

Use GNU 'date' for this. It will also convert between other formats for you (see date --help for the list of formats).

$ date -d 2013-07-05 +%m/%d/%Y
07/05/2013
$ date -d 07/05/2013 +%F
2013-07-05
share|improve this answer
    
to be more clear, I have the file file1 name: date 2000-01-01 2000-01-02 2000-01-03 2000-01-04 .. Required output is 01/01.2000 01/02/2000 01/03/2000 01/02/2000 and vice versal –  Abraham Sep 5 '13 at 19:12
    
@Abraham how does this not meet that requirement? –  jordanm Sep 5 '13 at 19:23

Assuming you have dates in text form that need to be converted

echo '2013-12-13' | awk -v FS=- -v OFS=/ '{print $2,$3,$1}'

And vice-versa

echo '12/13/2013' | awk -v FS=/ -v OFS=- '{print $3,$1,$2}'
share|improve this answer
    
to be more clear, I have the file file1 name: date 2000-01-01 2000-01-02 2000-01-03 2000-01-04 .. Required output is 01/01.2000 01/02/2000 01/03/2000 01/02/2000 and vice versal –  Abraham Sep 5 '13 at 19:12
    
Thank you 1_CR, It is working now after I modified a little bit!! #!/bin/bash echo '$1-$2-$3' | awk -v FS=- -v OFS=/ '{print $2,$3,$1}' data.file –  Abraham Sep 5 '13 at 19:31
    
@Abraham, you should try your luck with drewbenn's date approach, that is certainly more elegant –  1_CR Sep 5 '13 at 20:14
$ echo YYYY-MM-DD | { IFS=- read y m d && echo "$m/$d/$y"; }
MM/DD/YYYY

If you have a file with a lot of those dates, one per line:

awk -F- -vOFS=/ '{print $2,$3,$1}' < that-file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.