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

查看:161
本文介绍了如何从命令行更改日期的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以一种格式转换日期的最快方法是:

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


2008-06-01

2008-06-01

到另一种格式的日期,例如

to a date in another format, say


Sun 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

此选项在Linux上可用,但在达尔文上不可用。在达尔文,您可以改用以下语法:

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.

这篇关于如何从命令行更改日期的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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