Convert 12-hour date/time to 24-hour date/time

2019-01-04 13:57发布

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.

8条回答
爷、活的狠高调
2楼-- · 2019-01-04 14:27

It is a 1-line thing in python:

time.strftime('%H:%M:%S', time.strptime(x, '%I:%M %p'))

Example:

>>> time.strftime('%H:%M:%S', time.strptime('08:01 AM', '%I:%M %p'))
'08:01:00'
>>> time.strftime('%H:%M:%S', time.strptime('12:01 AM', '%I:%M %p'))
'00:01:00'
查看更多
爷的心禁止访问
3楼-- · 2019-01-04 14:29

This might be too simple thinking, but why not import it into excel, select the entire column and change the date format, then re-export as a tab delimited file? (I didn't test this, but it somehow sounds logical to me :)

查看更多
登录 后发表回答