Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line 如何从命令行更改日期的格式?_Command Line_Date_Format - Fatal编程技术网

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

Command line 如何从命令行更改日期的格式?,command-line,date,format,Command Line,Date,Format,例如,将日期转换为一种格式的最快方法是什么 2008-06-01 以另一种格式的日期,例如 2008年6月1日星期日 重要的一点实际上是“太阳”,因为根据日名的不同,我可能需要摆弄周围的其他东西——以一种不确定的方式。我正在运行GNUBash,版本3.2.17(1)-发行版(i386-apple-darwin9.0) [背景:我想从命令行执行此操作的原因是,我真正想要的是将其写入TextMate命令中……这是一项烦人的任务,我必须一直在TextMate中执行。] date -d yyyy-mm-

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

2008-06-01

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

2008年6月1日星期日

重要的一点实际上是“太阳”,因为根据日名的不同,我可能需要摆弄周围的其他东西——以一种不确定的方式。我正在运行GNUBash,版本
3.2.17(1)-发行版(i386-apple-darwin9.0)

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

date -d yyyy-mm-dd
如果希望对格式进行更多控制,也可以按如下方式添加:

date -d yyyy-mm-dd +%a
只是为了得到你想要的阳光部分

$ date -d '2005-06-30' +'%a %F'
Thu 2005-06-30
有关其他格式选项,请参见
man date

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

date -j -f "%Y-%m-%d" 2006-06-30 +"%a %F"
-f参数指定输入格式,+参数指定输出格式

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

date -d ... 
似乎没有削减它,因为我得到了一个使用错误:

usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
我正在运行GNUBash,3.2.17(1)版发行版(i386-apple-darwin9.0),就这个人而言,date-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.
阅读日期(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. -j不要试图设定日期。这允许您使用-f标志
除了+选项,还可以将一种日期格式转换为另一种格式。感谢您的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

谢谢。

如果你只是想得到一周中的某一天,不要尝试匹配字符串。当语言环境发生变化时,这种情况就会中断。
%u
格式为您提供日数:

 $ date -j -f "%Y-%m-%d" "2008-01-03" +"%u"
 4
事实上,那是一个星期四。您可以使用该数字索引到程序中的数组中,或者只使用该数字本身


有关更多详细信息,请参阅日期和strftime手册页。不过,OS X上的日期手册页是错误的,因为它没有列出这些有效的选项。

对不起,我只是遇到了一个使用错误。运行GNUBash,版本3.2.17(1)-发行版(i386-apple-darwin9.0),这可以在我的Debian机器上运行。显然,苹果电脑的情况并非如此。抱歉。事实上,在OSX上,你不会在date(1)主页上看到这一点。真的吗?评论系统使用的标记与答案不同?嗯,苹果的联机手册页与他们安装的手册页不同。我也注意到了这一点。