I need to set the date of a Linux box with a string I get from a program. This string is formatted as ddmmyy. Is it possible to use that directly or do I need to reformat it first?

Currently I have tried the following without any luck:

 date +%d%m%y -s "071012"

This should be 7. Oct 2012 but it is interpreted backwards and then becomes 12. Oct 07.

If I try to tell date to use the reverse format:

date +%y%m%d -s "071012"

it will still read it as 12. Oct 07. I would rather not have reverse the string because I thought date would be able to take care of that.


The + set the output format only. You need to pre-format your string is you want to use the -s option (either by modifying your program or by using e.g. sed).

In your case, you can pipe the result of your program in this:

sed -e "s/\(..\)\(..\)\(..\)/\\3\\2\\1/"
| improve this answer | |

Store the date in a shell variable, and you could set the date like this:

date -s "${dt:4:2}${dt:2:2}${dt:0:2}"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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