I'm facing some trouble with the date command. The following execution issues an error:

danilo@desktop:~$ x=$(date -d "+60 seconds"); dt=$(date -d "$x")
date: invalid date ‘Mo 11. Sep 09:07:05 CEST 2017’

This is strange, because it works in other computers I tested. Even this:

danilo@desktop:~$ x=$(date); dt=$(date -d "$x")
date: invalid date ‘Mo 11. Sep 09:06:43 CEST 2017’

Produces an error.

What is the reason for this error? Is it the timezone settings? How can I make it work?

  • It was empty before. It's being created with the results of the date command on the first part of the command. – Danilo M. Oliveira Sep 11 '17 at 7:09
  • Another way to express this is simply: $ date -d "$(date)". It's giving an error on the computer I'm testing, but it works in another that I have access via ssh and tested now. – Danilo M. Oliveira Sep 11 '17 at 7:10
  • there is no question in this post – Anthon Sep 11 '17 at 7:12
  • Sorry, I'll edit this. – Danilo M. Oliveira Sep 11 '17 at 7:16

The default format for your locale is not supported as input to date. The solution is to use some standard format. For example:

x=$(date -d "+60 seconds" +%s); dt=$(date -d "@$x")

+%s tells date to return a standard Unix format: seconds-since-epoch.

The @ sign in date -d "@$x" tells date to interpret $x as seconds-since-epoch.

  • I understand now. This works fine, thank you. Do you know how to fix the locale? – Danilo M. Oliveira Sep 11 '17 at 7:20
  • 1
    @DaniloM.Oliveira You can change the locale by changing the shell variable LC_TIME. For example, export LC_TIME=C will tell date to use the common C locale (and will make sure the variable is exported so that subshells see it as well. – John1024 Sep 11 '17 at 7:25

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.