Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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/"
share|improve this answer

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

dt=`your_program`
date -s "${dt:4:2}${dt:2:2}${dt:0:2}"
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.