将日期时间字符串转换为Bash中的纪元 [英] Convert date time string to epoch in Bash

查看:68
本文介绍了将日期时间字符串转换为Bash中的纪元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

日期时间字符串的格式如下:06/12/2012 07:21:22.如何将其转换为UNIX时间戳或纪元?

The date time string is in the following format: 06/12/2012 07:21:22. How can I convert it to UNIX timestamp or epoch?

推荐答案

您正在寻找的是date --date='06/12/2012 07:21:22' +"%s".请记住,这假设您使用的是GNU coreutils,因为--date%s格式字符串都是GNU扩展. POSIX没有指定任何一个,因此即使在POSIX兼容的系统上也没有可移植的方式进行这种转换.

What you're looking for is date --date='06/12/2012 07:21:22' +"%s". Keep in mind that this assumes you're using GNU coreutils, as both --date and the %s format string are GNU extensions. POSIX doesn't specify either of those, so there is no portable way of making such conversion even on POSIX compliant systems.

请咨询其他版本的date的相应手册页.

Consult the appropriate manual page for other versions of date.

注意:bash --date-d选项要求使用美国或ISO8601格式的日期,即mm/dd/yyyyyyyy-mm-dd,而不是英国,欧盟或其他任何格式.

Note: bash --date and -d option expects the date in US or ISO8601 format, i.e. mm/dd/yyyy or yyyy-mm-dd, not in UK, EU, or any other format.

这篇关于将日期时间字符串转换为Bash中的纪元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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