Linux 如何将 YYYYMMDDHHMMSS 转换为“date”可读的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4197606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:59:35  来源:igfitidea点击:

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

linuxbashdatesed

提问by User1

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

我有一组 YYYYMMDDHHMMSS 格式的日期/时间字符串,我想将其转换为date实用程序可读的内容。通常,我可以执行以下操作:

date -d "2010-10-01 12:34:56"

date -d "2010-10-01 12:34:56"

However, datedoes not like the YYYYMMDDHHMMSS:

但是,date不喜欢YYYYMMDDHHMMSS:

date -d "20100101123456"..invalid date

date -d "20100101123456"..失效日期

So, I probably need to refine the string to be in the prior format. I'm thinking sedis 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?

因此,我可能需要将字符串细化为先前的格式。我想sed是答案,但它变得非常难看。我很确定我的字符串将是正确的格式,那么如何轻松转换它们?

采纳答案by chris

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

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

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

回答by Ulrich Schwarz

 sed -ne 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)/-- ::/p'

I admit it'S a mouthful. All the .'s should optimally be [0-9] or \d, though I don't remember if sed supports the latter.

我承认这是一口。所有的 . 最好是 [0-9] 或 \d,但我不记得 sed 是否支持后者。

回答by Cascabel

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

如果格式是完全固定的,你可以在 bash 中完成它,切碎字符串:

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.

我不会费心尝试使用正则表达式 - 就像你说的那样,模式很快就会变得丑陋。很多重复([0-9]{4}),即使使用扩展或 perl 正则表达式。或者你可以灵活地匹配.;没有验证。

回答by uzi

What's with all of these regular expression answers? The date(1) tool has the ability to use strftime() style date formatting... an an example of converting one date type to another:

所有这些正则表达式的答案是什么?date(1) 工具能够使用 strftime() 样式的日期格式……一个将一种日期类型转换为另一种日期类型的示例:

$ date -j -f "%Y%m%d%H%M%S" "20100101123456" "+%Y-%m-%d %H:%M:%S"
2010-01-01 12:34:56

So if it's not in the format you want, convert it like that and then use it. If you just want to set it, you can simply do:

因此,如果它不是您想要的格式,请像这样转换然后使用它。如果你只是想设置它,你可以简单地做:

$ date -f "%Y%m%d%H%M%S" "20100101123456"

回答by Paused until further notice.

Try this:

尝试这个:

echo "20101106213245" | sed -r 's/^.{8}/& /;:a; s/([ :])(..)\B/:/;ta'

Result:

结果:

20101106 21:32:45
  • Insert a space after the eighth character
  • [label a] After a space or colon and the next two characters, add a colon
  • If a replacement was made, goto label a
  • 在第八个字符后插入一个空格
  • [label a] 在空格或冒号和接下来的两个字符后,添加一个冒号
  • 如果进行了更换,请转到标签 a

You want some hyphens, too?

你也想要一些连字符吗?

echo "20101106213245" | sed -r 's/^.{4}/&-/;:a; s/([-:])(..)\B/:/;ta;s/:/-/;s/:/ /'

Result:

结果:

2010-11-06 21:32:45
  • Insert a hyphen after the fourth character
  • [label a] After a hyphen or colon and the next two characters, add a colon
  • If a replacement was made, goto label a
  • Change the first colon to a hyphen (2010-11:06:21:32:45-> 2010-11-06:21:32:45)
  • Change the next colon to a space (2010-11-06:21:32:45-> 2010-11-06 21:32:45)
  • 在第四个字符后插入一个连字符
  • [label a] 在连字符或冒号以及接下来的两个字符之后,添加一个冒号
  • 如果进行了更换,请转到标签 a
  • 将第一个冒号更改为连字符 ( 2010-11:06:21:32:45-> 2010-11-06:21:32:45)
  • 将下一个冒号更改为空格 ( 2010-11-06:21:32:45-> 2010-11-06 21:32:45)

回答by Isaac

Either busybox date or bsd date accept a description of the input format.

busybox date 或 bsd date 都接受输入格式的描述。

Busybox is a GNU small utility, easy to install.

Busybox 是一个 GNU 小工具,易于安装。

The bsd format has been covered in another answer, so here is busybox:

bsd 格式已在另一个答案中介绍,所以这里是busybox:

$ busybox date -D "%Y%m%d%H%M%S" -d "20100101123456" +'%Y-%m-%d %H:%M:%S'
2010-01-01 12:34:56