Unix将月份名称转换为数字 [英] Unix convert Month name to number

查看:84
本文介绍了Unix将月份名称转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BASH Shell脚本中或使用gdate,给定日期为"Oct 2011",我如何转换为年月数字格式?例如,输出应为"2011-10".

In BASH shell scripting or using gdate, given a date like "Oct 2011" how do I convert to a year-month number format? Output should be "2011-10", for example.

推荐答案

mydate="Oct 2011"
date --date="$(printf "01 %s" $mydate)" +"%Y-%m"

GNU日期的parse_datetime接口(该示例使用的接口)具有很多规则.日期的2011年10月格式不是其中之一,因此您在日期的前面加一个"01",日期就喜欢它.

The parse_datetime interface for GNU date (which is what the example uses) has lots of rules. the Oct 2011 form of the date isn't one of them, so you prepend a "01 " to the front of it and date likes it.

这篇关于Unix将月份名称转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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