如何转换诸如“19-FEB-12”的字符串到UNIX的时代 [英] How to convert strings like "19-FEB-12" to epoch date in UNIX
本文介绍了如何转换诸如“19-FEB-12”的字符串到UNIX的时代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
19-FEB-12
16- FEB-12
05-AUG-09
我需要将这些日期与
解决方案
将时间转换为秒数:
date --date =19-FEB-12+%s
当前时期:
date +%s
所以,因为你的日期是过去的:
NOW =`date +%s`
THEN =`date --date =19-FEB-12+%s`
让DIFF = $ NOW- $ THEN
echo的区别是:$ DIFF
使用BSD的日期
命令,您需要
$ date -j -f%d-%B-%y19-FEB-12 +%s
与GNU的区别 date
:
-
-j
preventdate
从尝试设置时钟 - 必须使用
-f $ c显式设置输入格式$ c>
- 输入日期是一个常规参数,而不是一个选项
- 如果没有时间指定日期,请使用当前时间,而不是午夜的
。 li>
In UNIX how to convert to epoch milliseconds date strings like:
19-FEB-12
16-FEB-12
05-AUG-09
I need this to compare these dates with the current time on the server.
解决方案
To convert a date to seconds since the epoch:
date --date="19-FEB-12" +%s
Current epoch:
date +%s
So, since your dates are in the past:
NOW=`date +%s`
THEN=`date --date="19-FEB-12" +%s`
let DIFF=$NOW-$THEN
echo "The difference is: $DIFF"
Using BSD's date
command, you would need
$ date -j -f "%d-%B-%y" 19-FEB-12 +%s
Differences from GNU date
:
-j
preventsdate
from trying to set the clock- The input format must be explicitly set with
-f
- The input date is a regular argument, not an option (viz.
-d
) - When no time is specified with the date, use the current time instead of midnight.
这篇关于如何转换诸如“19-FEB-12”的字符串到UNIX的时代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文