0

How to change this time output ?

date --date="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"

Output: za apr 12 00:25:00 CEST 2014

Should output in this layout: %Y%m%d%H%M

How to implement this in the string ?

Thanks!!

7
  • Thanks but that gave me a really strange output, i have tryd that before.: date: invalid date ‘@2014041122311397255100’ Commented Apr 11, 2014 at 22:41
  • Can you please show your expected output? Commented Apr 11, 2014 at 22:43
  • za apr 12 00:35:00 CEST 2014 201404120035 Commented Apr 11, 2014 at 22:45
  • 2
    d="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d"` `date --date="$d" +%Y%m%d%H%M` Commented Apr 11, 2014 at 22:48
  • Thank you this works, but the time format is not UTC i see... it makes no sense if i change UTC is something else. date --date="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)" +%Y%m%d%H%M Commented Apr 11, 2014 at 22:57

1 Answer 1

1

i think this should do what you want:

d="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d"` `date --date="$d" +%Y%m%d%H%M`

d="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d" --utc` `date --date="$d" +%Y%m%d%H%M --utc`

Second one is in UTC.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow this works for me, you make me a very happy person!! Last question, how to get this in a variable ? When i echo $d it returns a strange string! @1397257200
Yeah, that's what you pass to date, to get it into a variable you do var=$(d="@$(echo $(TZ=UTC date +%s) - $(date +%s)'%(5*60)-(5*60)' | bc)"&&echo `date --date="$d" --utc` `date --date="$d" +%Y%m%d%H%M --utc`).
Oh, and by the way, you don't need TZ=UTC, since date +%s returns seconds since 1 January 1970 UTC, so it's the same, regardless if you specify a timezone there. What the internal command does is get the reminder from date +%s/300(5 minutes), subtract that from date +%s and then subtract 300 from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.