0

I need to change a date String from format YYYYMMDD to date YYYY-MM-DD without using date -d command. The only option I found is string manipulations.

  • How many tools can you name that perform text manipulation? If none, please go and do some basic research. Otherwise, tell us what you’ve tried. … … … … … … … … … … … … … … … … …  P.S. Technically, you have not asked a question. – G-Man Says 'Reinstate Monica' Apr 2 '18 at 15:58
3

What about using bash parameter expansion?

$ var=20180402
$ echo "${var::4}-${var:4:2}-${var:6}"
2018-04-02
| improve this answer | |
  • Spot of code golf: the last term could just be '${var:6}' – bu5hman Apr 2 '18 at 17:10
  • Further golf : the first term could be just ${var::4} – steve Apr 2 '18 at 18:33
  • Thanks didn't think about either. I did consider using ${var: -2} for the last one but didn't want to lead to confusion. – jesse_b Apr 2 '18 at 19:17

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.