How to convert YYYYMMDDHHMMSS to a date readable by `date`

前端 未结 6 382
执笔经年
执笔经年 2020-12-07 04:21

I have a set of date/time strings in the YYYYMMDDHHMMSS format that I want to convert to something readable by the date utility. Usually, I can do something li

6条回答
  •  佛祖请我去吃肉
    2020-12-07 04:35

    If the format is totally fixed, you could just do it within bash, chopping up the string:

    d=20100101123456
    pretty_date="${d:0:4}-${d:4:2}-${d:6:2} ${d:8:2}:${d:10:2}:${d:12:2}"
    # 2010-01-01 12:34:56
    ...
    

    I wouldn't bother trying to use regex - like you said, the pattern gets ugly fast. A lot of repetition of ([0-9]{4}), even with extended or perl regex. Or you could be flexible and just match .; no verification.

提交回复
热议问题