将12小时的日期/时间转换为24小时的日期/时间 [英] Convert 12-hour date/time to 24-hour date/time

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

问题描述

我有一个制表符分隔文件,其中每个记录都有12小时格式的时间戳记字段:


mm / dd / yyyy hh :mm:ss [AM | PM]。


我需要快速将这些字段转换为24小时制:


mm / dd / yyyy HH:mm:ss。


最好的方法是什么?我正在Windows平台上运行,除了通常的Windows工具之外,我还可以访问sed,awk,perl,python和tcl。

解决方案

使用Perl和手工制作的正则表达式,而不是像strptime这样的设施:

 #!/ bin / perl -w 
while(<))
{
#对于不使用前导零的日期时间,请改用此正则表达式:
#(?:\d {1,2} / \d {1,2} / \d {4})(\d {1,2})(?:: \d\d:\d\d) (AM | PM)
while(m%(?: \d\d / \d\d / \d {4})(\d\d)(? \\ d \d:\d\d)(AM | PM)%)
{
我的$ hh = $ 1;
$ hh - = 12 if($ 2 eq'AM'& $ hh == 12);
$ hh + = 12 if($ 2 eq'PM'& $ hh!= 12);
$ hh = sprintf%02d,$ hh;
#对于不使用前导零的日期时间,请使用此正则表达式:
#(\d {1,2} / \d {1,2} / \d {4 })(\d {1,2})(:\d\d:\d\d)(?:AM | PM)
s%(\d\d / \ d \d / \d {4})(\d\d)(:\d\d:\d\d)(?:AM | PM)%$ 1 $ hh $ 3%;
}
print;
}

这很奇怪,但也可以每行转换多个时间戳。 >

请注意,AM / PM到24小时的转换不是微不足道的。




  • 12:01 - > 00:01

  • 12:01 PM - > 12:01

  • 01:30 AM - > 01 :30

  • 01:30 PM - > 13:30



现在测试: / p>

  perl ampm-24hr.pl<< ;! 
12/24/2005 12:01:00 AM
09/22/1999 12:00:00 PM
12/12/2005 01:15:00 PM
01 / 01/2009 01:56:45 AM
12/30/2009 10:00:00 PM
12/30/2009 10:00:00 AM


12/24/2005 00:01:00
09/22/1999 12:00:00
12/12/2005 13:15:00
01/01/2009 01:56:45
12/30/2009 22:00:00
12/30/2009 10:00:00






添加



什么是在AM / PM时间和JavaScript之间的24小时时间内转换的简单方法,为转换提供了一种替代算法:

  $ hh =($ 1%12)+(($ 2 eq'AM')?0:12); 

只需一个测试...可能更整洁。


I have a tab delimited file where each record has a timestamp field in 12-hour format:

mm/dd/yyyy hh:mm:ss [AM|PM].

I need to quickly convert these fields to 24-hour time:

mm/dd/yyyy HH:mm:ss.

What would be the best way to do this? I'm running on a Windows platform, but I have access to sed, awk, perl, python, and tcl in addition to the usual Windows tools.

解决方案

Using Perl and hand-crafted regexes instead of facilities like strptime:

#!/bin/perl -w
while (<>)
{
    # for date times that don't use leading zeroes, use this regex instead:
    # (?:\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(?::\d\d:\d\d) (AM|PM)
    while (m%(?:\d\d/\d\d/\d{4} )(\d\d)(?::\d\d:\d\d) (AM|PM)%)
    {
        my $hh = $1;
        $hh -= 12 if ($2 eq 'AM' && $hh == 12);
        $hh += 12 if ($2 eq 'PM' && $hh != 12);
        $hh = sprintf "%02d", $hh;
        # for date times that don't use leading zeroes, use this regex instead:
        # (\d{1,2}/\d{1,2}/\d{4} )(\d{1,2})(:\d\d:\d\d) (?:AM|PM)
        s%(\d\d/\d\d/\d{4} )(\d\d)(:\d\d:\d\d) (?:AM|PM)%$1$hh$3%;
    }
    print;
}

That's very fussy - but also converts possibly multiple timestamps per line.

Note that the transformation for AM/PM to 24-hour is not trivial.

  • 12:01 AM --> 00:01
  • 12:01 PM --> 12:01
  • 01:30 AM --> 01:30
  • 01:30 PM --> 13:30

Now tested:

perl ampm-24hr.pl <<!
12/24/2005 12:01:00 AM
09/22/1999 12:00:00 PM
12/12/2005 01:15:00 PM
01/01/2009 01:56:45 AM
12/30/2009 10:00:00 PM
12/30/2009 10:00:00 AM
!

12/24/2005 00:01:00
09/22/1999 12:00:00
12/12/2005 13:15:00
01/01/2009 01:56:45
12/30/2009 22:00:00
12/30/2009 10:00:00


Added:

In What is a Simple Way to Convert Between an AM/PM Time and 24 hour Time in JavaScript, an alternative algorithm is provided for the conversion:

$hh = ($1 % 12) + (($2 eq 'AM') ? 0 : 12);

Just one test...probably neater.

这篇关于将12小时的日期/时间转换为24小时的日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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