转换bash中的日期格式 [英] Convert date formats in bash

查看:159
本文介绍了转换bash中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样格式的日期:2011年6月27日,我想将其转换为20110627



是否可以在bash中执行?

解决方案

  #since这是昨天
date -dyesterday +%Y%m%d

#更精确,更推荐
date -d'27 JUN 2011'+%Y%m%d

#assuming这与昨天相似` date`你的问题
#http://stackoverflow.com/q/6497525/638649
日期-d'last-monday'+%Y%m%d

#@ onh的评论你可以这样做
DATE =2011年7月27日; date -d$ DATE+%Y%m%d

#or从stdin
读取它的方法read -pGet date>>DATE; printfAS YYYYMMDD format>>%s`date
-d$ DATE+%Y%m%d`

#然后输出以下内容:
#Get date>> 2011年6月27日
#AS YYYYMMDD格式>> 20110627

#如果你真的想使用awk
echo27六月2011| awk'{printdate -d\$ 1FS $ 2FS $ 3\+%Y%m%d}'| bash

#note | bash只是将awk的输出重定向到要执行的shell
#FS是字段分隔符,在这种情况下,您可以使用$ 0打印行
#But这是有用的,如果你有一个以上的日期行

更多日期



注意这只有工作在GNU日期



我已经读过:


Solaris版本的日期,这是不能
支持 -d 可以解决
替换sunfreeware.com版本的
日期



I have a date in this format: "27 JUN 2011" and I want to convert it to 20110627

Is it possible to do in bash?

解决方案

#since this was yesterday
date -dyesterday +%Y%m%d

#more precise, and more recommended
date -d'27 JUN 2011' +%Y%m%d

#assuming this is similar to yesterdays `date` question from you 
#http://stackoverflow.com/q/6497525/638649
date -d'last-monday' +%Y%m%d

#going on @seth's comment you could do this
DATE = "27 jun 2011"; date -d"$DATE" +%Y%m%d

#or a method to read it from stdin
read -p "  Get date >> " DATE; printf "  AS YYYYMMDD format >> %s"  `date
-d"$DATE" +%Y%m%d`    

#which then outputs the following:
#Get date >> 27 june 2011   
#AS YYYYMMDD format >> 20110627

#if you really want to use awk
echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash

#note | bash just redirects awk's output to the shell to be executed
#FS is field separator, in this case you can use $0 to print the line
#But this is useful if you have more than one date on a line

More on Dates

note this only works on GNU date

I have read that:

Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date

这篇关于转换bash中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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