55

I have a string in the format "yyyymmdd". It is a string in bash and I want to get it converted into a date so that all other date functions can be used on it.

"20121212" string into "20121212" date with format "%Y%m%d".

Yahoo-Me
  • 4,933
  • 5
  • 27
  • 26
  • 1
    bash doesn't have a date type. What are these "all other date functions" you're talking about? – Thomas Jun 21 '12 at 18:25
  • 1
    Say I want to add 7 days to a date. I think that can be done on bash. It is not simple integer addition at all. – Yahoo-Me Jun 21 '12 at 18:27
  • Possible duplicate of [Parse Date in Bash](http://stackoverflow.com/questions/1842634/parse-date-in-bash) – Dave Jarvis May 10 '16 at 02:01

4 Answers4

59

This worked for me :

date -d '20121212 7 days'
date -d '12-DEC-2012 7 days'
date -d '2012-12-12 7 days'
date -d '2012-12-12 4:10:10PM 7 days'
date -d '2012-12-12 16:10:55 7 days'

then you can format output adding parameter '+%Y%m%d'

Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • Does date support converting from ISO-8601 – CMCDragonkai Jul 14 '14 at 05:27
  • @CMCDragonkai `now=$(date -Im)` stores the current time as an ISO-8601 date/time to minute precision, and `date -d = $now` is happy with that format. I'm not sure though if the same is necessarily true for all of the variants which are ISO-8601 compatible. – mc0e Jun 16 '15 at 11:07
  • This is certainly useful, and gnu date also does things like converting time zones, but there's a lot of other date manipulation tasks one might want. E.g. Difference between dates to get an interval; Rounding of both dates and intervals to a given precision (round down, round up and round to nearest), or stretching things further, how would I find the date in 10 business days time from date X (given the holidays of a given country). Is there a more comprehensive tool suitable for scripting use? – mc0e Jun 16 '15 at 11:17
  • @mc0e Only ones I know of are language specific libraries, not CLI executables. Perhaps one could wrap them up into a CLI binary. Like Moment in JS and some other ones. – CMCDragonkai Jun 17 '15 at 08:02
  • @CMCDragonkai Yeah, I was thinking wrapping up an existing library with a CLI seemed like a good idea. That said, reading [this](http://www.unix.com/man-page/debian/3pm/date::manip::misc/) is worthwhile before anyone jumps in too quick. Probably the best solution would be a CLI around a C or C++ library, if there's a good one to hand. – mc0e Jun 17 '15 at 19:51
  • 5
    Why did you put "7 days" in all of your examples? It works without these data. Just `date -d '20121212' +%Y%m%d` will do what author wants – Victor Perov Oct 21 '17 at 14:44
  • 2
    @VictorPerov It's what the author asked for in the comments of his question. – Torsten Mar 20 '18 at 17:21
36

We can use date -d option

1) Change format to "%Y-%m-%d" format i.e 20121212 to 2012-12-12

date -d '20121212' +'%Y-%m-%d'

2)Get next or last day from a given date=20121212. Like get a date 7 days in past with specific format

date -d '20121212 -7 days' +'%Y-%m-%d'

3) If we are getting date in some variable say dat

dat2=$(date -d "$dat -1 days" +'%Y%m%d')
minhas23
  • 9,291
  • 3
  • 58
  • 40
12

date only work with GNU date (usually comes with Linux)

for OS X, two choices:

  1. change command (verified)

    #!/bin/sh
    #DATE=20090801204150
    #date -jf "%Y%m%d%H%M%S" $DATE "+date \"%A,%_d %B %Y %H:%M:%S\""
    date "Saturday, 1 August 2009 20:41:50"
    

    http://www.unix.com/shell-programming-and-scripting/116310-date-conversion.html

  2. Download the GNU Utilities from Coreutils - GNU core utilities (not verified yet) http://www.unix.com/emergency-unix-and-linux-support/199565-convert-string-date-add-1-a.html

Vanjor
  • 181
  • 1
  • 6
5

just use the -d option of the date command, e.g.

date -d '20121212' +'%Y %m'
johnshen64
  • 3,734
  • 1
  • 21
  • 17