Khaino
Khaino

Reputation: 4149

How to convert a specific format of string to date/seconds shell script linux

I need to convert a string of %y/%m/%d %H:%M:%S (e.g 18/04/25 16:04:00) to seconds.

date -d "18/04/25 16:04:00" "+%s" results in invalid format, though date -d "04/25/2018 16:04:00" "+%s" gives correct result.

Is it possible specify the format of string (i.e. 18/04/25 16:04:00) as I can do in OS X, date -j -f '%y/%m/%d %H:%M:%S' '18/04/25 16:04:00' +'%s'?

Upvotes: 0

Views: 979

Answers (1)

anubhava
anubhava

Reputation: 784918

You can add century at the start of your string to make it work in gnu date:

dt="18/04/25 16:04:00"
date -d "$(date '+%C')$dt" '+%s'

1524686640

$(date '+%C') will return current century i.e. 20

Upvotes: 1

Related Questions