格式时间最多为毫秒以在日志文件中使用
Format Time up to Milliseconds to use in log files
我的 VBScript 有一个日志文件,它使用 FormatDateTime
函数记录当前日期和时间的信息。
我喜欢将时间格式化为毫秒,并且采用以下格式:
MM/DD/YY hh:mm:ss:mss AM/PM
但是,不幸的是 FormatDateTime
不允许以这种方式格式化时间。
搜索这个之后,我找到了 this answer 并且它是关于如何使用 Timer
功能的,所以我无法记录时间来一次又一次地使用它来记录文件。
正如 W3schools 所述,
The Timer function returns the number of seconds since 12:00 AM.
但我希望我的日志文件甚至在 12:00 上午之前以上述格式记录时间,因此使用 Timer
函数不是最佳选择。
请告诉我一种在日志文件中正确执行此操作的方法。
vbscript 函数 Now()
将 return 这种格式的当前系统日期和时间 - 5/2/2017 9:45:34 AM
,但是如果你需要添加毫秒,你可以使用 Timer
- Timer math from here
'capture the date and timer together so if the date changes while
'the other code runs the values you are using don't change
t = Timer
dateStr = Date()
temp = Int(t)
milliseconds = Int((t-temp) * 1000)
seconds = temp mod 60
temp = Int(temp/60)
minutes = temp mod 60
hours = Int(temp/60)
label = "AM"
If hours > 12 Then
label = "PM"
hours = hours-12
End If
'format it and add the date
strTime = LeftPad(hours, "0", 2) & ":"
strTime = strTime & LeftPad(minutes, "0", 2) & ":"
strTime = strTime & LeftPad(seconds, "0", 2) & "."
strTime = strTime & LeftPad(milliseconds, "0", 3)
WScript.Echo dateStr & " " & strTime & " " & label
'this function adds characters to a string to meet the desired length
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function
做同样事情的另一种方法,代码更少。在这段代码中,我们将 Now()
拆分成一个数组,这样我们就可以将它用于除毫秒之外的所有内容。:
WScript.Echo PrintTimeStamp()
Function PrintTimeStamp()
nowParts = SPLIT(Now(), " ")
timePart = nowParts(1)
t = Timer
milliseconds = Int((t-Int(t)) * 1000)
PrintTimeStamp = nowParts(0) & " " & LeftPad(nowParts(1), "0", 8) & "." & LeftPad(milliseconds, "0", 3) & " " & nowParts(2)
End Function
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function
我的 VBScript 有一个日志文件,它使用 FormatDateTime
函数记录当前日期和时间的信息。
我喜欢将时间格式化为毫秒,并且采用以下格式:
MM/DD/YY hh:mm:ss:mss AM/PM
但是,不幸的是 FormatDateTime
不允许以这种方式格式化时间。
搜索这个之后,我找到了 this answer 并且它是关于如何使用 Timer
功能的,所以我无法记录时间来一次又一次地使用它来记录文件。
正如 W3schools 所述,
The Timer function returns the number of seconds since 12:00 AM.
但我希望我的日志文件甚至在 12:00 上午之前以上述格式记录时间,因此使用 Timer
函数不是最佳选择。
请告诉我一种在日志文件中正确执行此操作的方法。
vbscript 函数 Now()
将 return 这种格式的当前系统日期和时间 - 5/2/2017 9:45:34 AM
,但是如果你需要添加毫秒,你可以使用 Timer
- Timer math from here
'capture the date and timer together so if the date changes while
'the other code runs the values you are using don't change
t = Timer
dateStr = Date()
temp = Int(t)
milliseconds = Int((t-temp) * 1000)
seconds = temp mod 60
temp = Int(temp/60)
minutes = temp mod 60
hours = Int(temp/60)
label = "AM"
If hours > 12 Then
label = "PM"
hours = hours-12
End If
'format it and add the date
strTime = LeftPad(hours, "0", 2) & ":"
strTime = strTime & LeftPad(minutes, "0", 2) & ":"
strTime = strTime & LeftPad(seconds, "0", 2) & "."
strTime = strTime & LeftPad(milliseconds, "0", 3)
WScript.Echo dateStr & " " & strTime & " " & label
'this function adds characters to a string to meet the desired length
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function
做同样事情的另一种方法,代码更少。在这段代码中,我们将 Now()
拆分成一个数组,这样我们就可以将它用于除毫秒之外的所有内容。:
WScript.Echo PrintTimeStamp()
Function PrintTimeStamp()
nowParts = SPLIT(Now(), " ")
timePart = nowParts(1)
t = Timer
milliseconds = Int((t-Int(t)) * 1000)
PrintTimeStamp = nowParts(0) & " " & LeftPad(nowParts(1), "0", 8) & "." & LeftPad(milliseconds, "0", 3) & " " & nowParts(2)
End Function
Function LeftPad(str, addThis, howMany)
LeftPad = String(howMany - Len(str), addThis) & str
End Function