如何转换诸如“19-FEB-12”的字符串到UNIX的时代 [英] How to convert strings like "19-FEB-12" to epoch date in UNIX

查看:97
本文介绍了如何转换诸如“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


  1. -j prevent date 从尝试设置时钟

  2. 必须使用 -f

  3. 输入日期是一个常规参数,而不是一个选项

  4. 如果没有时间指定日期,请使用当前时间,而不是午夜的
    。 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:

  1. -j prevents date from trying to set the clock
  2. The input format must be explicitly set with -f
  3. The input date is a regular argument, not an option (viz. -d)
  4. When no time is specified with the date, use the current time instead of midnight.

这篇关于如何转换诸如“19-FEB-12”的字符串到UNIX的时代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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