我从文件中获取输入。时间可能是军事或标准(上午/下午)
我需要将时间转换为军事。
以下是CSV文件中的时间示例
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
答案 0 :(得分:1)
首先,我不确定我是否正确理解了这个问题。
可能是这样的:
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
答案 1 :(得分:0)
这样的事情应该有效:
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)