如何将YYYYMMDDHHMMSS转换为`date`可读的日期 [英] How to convert YYYYMMDDHHMMSS to a date readable by `date`

查看:453
本文介绍了如何将YYYYMMDDHHMMSS转换为`date`可读的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组YYYYMMDDHHMMSS格式的日期/时间字符串,我想转换为 date 实用程序可读的东西。通常我可以这样做:

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 like:

date -d2010-10-01 12:34:56

但是, date 不喜欢YYYYMMDDHHMMSS:

However, date does not like the YYYYMMDDHHMMSS:

date -d20100101123456 ..无效日期

所以,我可能需要改进字符串以前的格式。我在想 sed 是答案,但它变得丑陋非常快。我很确定我的字符串将是正确的格式,那么如何轻松转换它们?

So, I probably need to refine the string to be in the prior format. I'm thinking sed is the answer, but it gets ugly very fast. I'm quite certain my strings will be the proper format, so how do I easily convert them?

推荐答案

日期不允许YYYYMMDDHHMMSS,但它是YYYYMMDD HH:MM:SS,所以:

date doesn't allow "YYYYMMDDHHMMSS", but it does "YYYYMMDD HH:MM:SS", so:

D="20100101123456"
date -d "${D:0:8} ${D:8:2}:${D:10:2}:${D:12:2}"

这篇关于如何将YYYYMMDDHHMMSS转换为`date`可读的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆