如何从命令行更改日期格式?

时间:2022-04-01 01:43:08

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

比如说,以一种格式转换日期的最快方法是什么?

2008-06-01

to a date in another format, say

以另一种格式表示日期

Sun 1st June 2008

Sun 2008年6月1日

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).

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

[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.]

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

6 个解决方案

#1


8  

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

See man date for other format options.

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

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

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

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

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

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

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.

正如下面另一张海报所指出的,你最好使用%u(数周日)而不是%a来避免本地化问题。

#2


1  

Reading the date(1) manpage would have revealed:

阅读日期(1)联机帮助页会显示:

-j   Do not try to set the date.  This allows you to use the -f flag
     in addition to the + option to convert one date format to another.

#3


1  

Thanks for that sgm. So just so I can come back to refer to it -

谢谢你的sgm。所以我可以回来参考它 -

date -j -f "%Y-%m-%d" "2008-01-03" +"%a%e %b %Y"
            ^               ^        ^
            parse using     |        output using
            this format     |        this format
                            |
                       date expressed in
                       parsing format

Thu 3 Jan 2008

Thanks.

#4


0  

date -d yyyy-mm-dd

If you want more control over formatting, you can also add it like this:

如果您想要更多地控制格式,您也可以像这样添加它:

date -d yyyy-mm-dd +%a

to just get the Sun part that you say you want.

只是得到你说你想要的太阳部分。

#5


0  

date -d ... 

doesn't seem to cut it, as I get a usage error:

似乎没有削减它,因为我得到一个用法错误:

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0), and as far as the man goes, date -d is just for

我正在运行GNU bash,版本3.2.17(1)-release(i386-apple-darwin9.0),就该人而言,日期-d仅适用于

-d dst  Set the kernel's value for daylight saving time.  If dst is non-
         zero, future calls to gettimeofday(2) will return a non-zero for
         tz_dsttime.

#6


0  

If you're just looking to get the day of the week, don't try to match strings. That breaks when the locale changes. The %u format give you the day number:

如果您只是想要获得一周中的某一天,请不要尝试匹配字符串。当语言环境发生变化时会中断。 %u格式为您提供日期编号:

 $ date -j -f "%Y-%m-%d" "2008-01-03" +"%u"
 4

And indeed, that was a Thursday. You might use that number to index into an array you have in your program, or just use the number itself.

事实上,这是星期四。您可以使用该数字索引到程序中的数组,或者只使用数字本身。

See the date and strftime man pages for more details. The date manpage on OS X is the wrong one, though, since it doesn't list these options that work.

有关更多详细信息,请参见date和strftime手册页。但是,OS X上的日期联机帮助页是错误的,因为它没有列出这些有效的选项。

#1


8  

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

See man date for other format options.

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

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

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

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

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

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

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.

正如下面另一张海报所指出的,你最好使用%u(数周日)而不是%a来避免本地化问题。

#2


1  

Reading the date(1) manpage would have revealed:

阅读日期(1)联机帮助页会显示:

-j   Do not try to set the date.  This allows you to use the -f flag
     in addition to the + option to convert one date format to another.

#3


1  

Thanks for that sgm. So just so I can come back to refer to it -

谢谢你的sgm。所以我可以回来参考它 -

date -j -f "%Y-%m-%d" "2008-01-03" +"%a%e %b %Y"
            ^               ^        ^
            parse using     |        output using
            this format     |        this format
                            |
                       date expressed in
                       parsing format

Thu 3 Jan 2008

Thanks.

#4


0  

date -d yyyy-mm-dd

If you want more control over formatting, you can also add it like this:

如果您想要更多地控制格式,您也可以像这样添加它:

date -d yyyy-mm-dd +%a

to just get the Sun part that you say you want.

只是得到你说你想要的太阳部分。

#5


0  

date -d ... 

doesn't seem to cut it, as I get a usage error:

似乎没有削减它,因为我得到一个用法错误:

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

I'm running GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0), and as far as the man goes, date -d is just for

我正在运行GNU bash,版本3.2.17(1)-release(i386-apple-darwin9.0),就该人而言,日期-d仅适用于

-d dst  Set the kernel's value for daylight saving time.  If dst is non-
         zero, future calls to gettimeofday(2) will return a non-zero for
         tz_dsttime.

#6


0  

If you're just looking to get the day of the week, don't try to match strings. That breaks when the locale changes. The %u format give you the day number:

如果您只是想要获得一周中的某一天,请不要尝试匹配字符串。当语言环境发生变化时会中断。 %u格式为您提供日期编号:

 $ date -j -f "%Y-%m-%d" "2008-01-03" +"%u"
 4

And indeed, that was a Thursday. You might use that number to index into an array you have in your program, or just use the number itself.

事实上,这是星期四。您可以使用该数字索引到程序中的数组,或者只使用数字本身。

See the date and strftime man pages for more details. The date manpage on OS X is the wrong one, though, since it doesn't list these options that work.

有关更多详细信息,请参见date和strftime手册页。但是,OS X上的日期联机帮助页是错误的,因为它没有列出这些有效的选项。