Linux 是否有没有空格的日期/时间格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4018503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a date/time format that does not have spaces?
提问by User1
I have a CGI script that will convert a given string to a date/time using the unix date
command. I'm looking for a format that can easily be embedded to a URL without the need for escaping with a %20
. The client that is building the date/time into the URL does not have a conversion to unix time (seconds since epoch) and does not have a way to convert to the offset from zulu (ISO8601 will not work). However, it is possible to reformat the date/time used to build the URL in many other ways.
我有一个 CGI 脚本,可以使用 unixdate
命令将给定的字符串转换为日期/时间。我正在寻找一种可以轻松嵌入到 URL 中而无需使用%20
. 将日期/时间构建到 URL 中的客户端无法转换为 unix 时间(自纪元以来的秒数),并且无法转换为来自 zulu 的偏移量(ISO8601 不起作用)。但是,可以通过许多其他方式重新格式化用于构建 URL 的日期/时间。
Are there any other options to build a datetime in a non-spaced format?
是否还有其他选项可以以非间隔格式构建日期时间?
采纳答案by User1
I found a simple work around. Simply use underscores for spaces and do a tr
in the CGI script before converting to a date. It looks something like this:
我找到了一个简单的解决方法。只需使用下划线作为空格并tr
在转换为日期之前在 CGI 脚本中执行 a。它看起来像这样:
stamp="$(echo $stamp|tr _ ' '|xargs -0 date -d)"
stamp="$(echo $stamp|tr _ ' '|xargs -0 date -d)"
Then use a date that looks something like this:
然后使用如下所示的日期:
26_Oct_2010_11:57:56_CDT
26_Oct_2010_11:57:56_CDT
which converts to:
转换为:
date -d "26 Oct 2010 11:57:56 CDT"
date -d "26 Oct 2010 11:57:56 CDT"
回答by chrisaycock
$ date "+%F-%T"
2010-10-25-16:23:14