How do I convert the time to military time?

蓝咒 提交于 2019-12-24 08:55:07

问题


I am getting input from a file. The time maybe military or standard (AM/PM)

I need to convert the time to military.

Here are some examples of the time in the CSV file

2011-08-16, 2:28:00 PM, 15:28
2011-08-16, 1:21:00 PM, 13:28
2011-08-16, 2:13:00 PM, 16:28

回答1:



Firstly I'm not sure whether I understood the question correctly. Could be like this:

Option Explicit

Const SOURCE_PATH = "C:\source.csv"
Const DEST_PATH = "C:\destination.csv"
Dim oReg, oFso

Set oReg = New RegExp
oReg.IgnoreCase = True
oReg.Global = True
oReg.Pattern = "\b((1[0-2]|[1-9]):[0-5][0-9]:[0-5][0-9] [AP]M)\b"

Function cb_MilTime(a, b, c, d, e)
    cb_MilTime = FormatDateTime(CDate(b), 4)
    'cb_MilTime = Replace(cb_MilTime, ":", "") 'need seperator?
End Function

Set oFso = CreateObject("Scripting.FileSystemObject")

If oFso.FileExists(SOURCE_PATH) Then 
    oFso.OpenTextFile(DEST_PATH, 2, True).Write(oReg.Replace(_
    oFso.OpenTextFile(SOURCE_PATH).ReadAll(), GetRef("cb_MilTime")))
Else 
    Err.Raise 8, "Source path does not exists"
End If

WScript.Echo "File Saved to "& DEST_PATH

Set oFso = Nothing
Set oReg = Nothing



回答2:


Something like this should work:

Const militaryTimeFormat As String = "HH:mm"

' convert to a string in military time
Dim input As String = DateTime.Now.ToString(militaryTimeFormat)

' convert from a string in military time
Dim time As DateTime = DateTime.ParseExact(input, militaryTimeFormat, Nothing)


来源:https://stackoverflow.com/questions/7083986/how-do-i-convert-the-time-to-military-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!