如何从命令行更改日期格式? [英] How can I change the format of a date from the command line?

查看:2

问题描述

将日期转换为一种格式的最快方法是什么

What's the quickest way to convert a date in one format, say

2008-06-01

到另一种格式的日期,比如说

to a date in another format, say

2008 年 6 月 1 日星期日

Sun 1st June 2008

重要的一点实际上是太阳",因为根据日期名称,我可能需要摆弄其他东西 - 以一种不确定的方式.我正在运行 GNU bash,版本 3.2.17(1)-release (i386-apple-darwin9.0).

The important bit is actually the 'Sun' because depending on the dayname, I may need to fiddle other things around - in a non-deterministic fashion. I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0).

[背景:我想从命令行执行此操作的原因是,我真正想要的是将其写入 TextMate 命令......这是我必须在 textMate 中一直执行的一项烦人的任务.]

[Background: The reason that I want to do it from the command line, is that what I really want is to write it into a TextMate command... It's an annoying task I have to do all the time in textMate.]

推荐答案

$ date -d '2005-06-30' +'%a %F'
Thu 2005-06-30

有关其他格式选项,请参阅 man date.

See man date for other format options.

此选项在 Linux 上可用,但在 Darwin 上不可用.在 Darwin 中,您可以改用以下语法:

This option is available on Linux, but not on Darwin. In Darwin, you can use the following syntax instead:

date -j -f "%Y-%m-%d" 2006-06-30 +"%a %F"

-f 参数指定输入格式,+ 参数指定输出格式.

The -f argument specifies the input format and the + argument specifies the output format.

正如下面另一张海报所指出的,您最好使用 %u(数字星期几)而不是 %a 以避免本地化问题.

As pointed out by another poster below, you would be wise to use %u (numeric day of week) rather than %a to avoid localization issues.

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