重击以来多少天 [英] Bash how many days since

查看:45
本文介绍了重击以来多少天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下日期格式,这是数据库查询结果:

I have the following format of Date which is a database query result:

yyyy-mm-dd-hh.mm.ss.mmmmmm

示例

myDate=2014-03-28-23.05.04.000000

我尝试了

$(( ( $(date +%s) - $(date -d "${myDate}" +%s) ) /(24 * 60 * 60 ) ))

但是它给我日期无效的错误.

But it gives me date not valid error.

我想知道距myDate已有多少天了.我需要不对结果进行四舍五入,例如,根据小时,分钟和秒数,结果可能是2.5天或1.6等.

I want to find out how many days its been since myDate. I need to have the result not rounded up or down as an example the result could be 2.5 days or 1.6 etc. based on the number of hours, minutes and seconds.

推荐答案

只需稍微调整一下分隔符即可:

Just need to tweak the separators a bit:

$ date -d "$(sed 's/-/ /3; s/\./:/1; s/\./:/1' <<< $myDate)" +%s
1396062304

将第三个连字符更改为空格,然后将第一个点更改为冒号,然后将(现在)第一个点更改为冒号.

Change the 3rd hyphen to a space, and then change the first dot to a colon, and then change the (now) first dot to a colon.

然后

$ echo $(( ( $(date +%s) - $(date -d "$( sed 's/-/ /3; s/\./:/1; s/\./:/1' <<< $myDate)" +%s) ) /(24 * 60 * 60 ) ))
2

这篇关于重击以来多少天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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