dateAdd seconds without AM/PM?
dateAdd seconds without AM/PM?
我正在尝试从日期中减去一定的秒数以便在其他地方使用。但是,我需要它是 24 小时格式。这是我的功能:
Function getDateTime(df) 'timediff
getDateTime = DateAdd("S",0 - df,Now)
End Function
需要10/5/2015 13:58:32
的格式而不是10/5/2015 1:58:32 PM
是否有一种简单的方法来转换它,或者我需要进行字符串评估和操作? (if hrs > 12 { etc... })
好吧,在任何编程语言(或 DBMS)中,日期都是 值 。它们在您的 IDE、脚本工具中的显示方式或从 DBMS 查询时的显示方式都无关紧要。它们实际上存储相同 "under the hood".
如果您希望它们以特定方式出现在报告中,例如 UI 等,则必须将它们转换为具有特定格式的字符串。再次重申,日期只是存储在内存中的数值。
当您说 "It needs to be in the format of 10/5/2015 13:58:32 instead of 10/5/2015 1:58:32 PM" 时,后一个值就是您的调试器显示它的方式(很可能使用具有默认日期格式的 ToString()
调用)。
这是我替换的函数:
Function getDateTime(df) 'timediff in seconds
getDateTime = FormatDateTime(DateAdd("S",0 - df,Now),vbGeneralDate)
getDateTime = Year(getDateTime) & "-" & Month(getDateTime) & "-" & Day(getDateTime)_
& " " & Hour(getDateTime) & ":" & Minute(getDateTime) & ":" & (Second(getDateTime))
End Function
感谢@Lankymart 指出我忽略的一个简单事实。请随意 post,我会接受你的答案。
在 another question 中处理了这个问题。
VBScript Date/Time 当您看到一种特定格式时,它被存储为一个整数,该格式只是运行时推断该 Date/Time 变量 的默认字符串表示(通常使用计算机区域设置).
Function FormatDate(df)
FormatDate = Right("00" & Day(df), 2) & _
"/" & Right("00" & Month(df), 2) & "/" & Year(df) & _
" " & Right("00" & Hour(df), 2) & ":" & _
Right("00" & Minute(df), 2) & ":" & Right("00" & Second(df), 2)
End Function
当您有日期 like 04/04/2015
例如 (只要 DBMS 支持)时,您还可以使用 Left(MonthName(Month(df)), 3)
来避免日期格式问题.
我正在尝试从日期中减去一定的秒数以便在其他地方使用。但是,我需要它是 24 小时格式。这是我的功能:
Function getDateTime(df) 'timediff
getDateTime = DateAdd("S",0 - df,Now)
End Function
需要10/5/2015 13:58:32
的格式而不是10/5/2015 1:58:32 PM
是否有一种简单的方法来转换它,或者我需要进行字符串评估和操作? (if hrs > 12 { etc... })
好吧,在任何编程语言(或 DBMS)中,日期都是 值 。它们在您的 IDE、脚本工具中的显示方式或从 DBMS 查询时的显示方式都无关紧要。它们实际上存储相同 "under the hood".
如果您希望它们以特定方式出现在报告中,例如 UI 等,则必须将它们转换为具有特定格式的字符串。再次重申,日期只是存储在内存中的数值。
当您说 "It needs to be in the format of 10/5/2015 13:58:32 instead of 10/5/2015 1:58:32 PM" 时,后一个值就是您的调试器显示它的方式(很可能使用具有默认日期格式的 ToString()
调用)。
这是我替换的函数:
Function getDateTime(df) 'timediff in seconds
getDateTime = FormatDateTime(DateAdd("S",0 - df,Now),vbGeneralDate)
getDateTime = Year(getDateTime) & "-" & Month(getDateTime) & "-" & Day(getDateTime)_
& " " & Hour(getDateTime) & ":" & Minute(getDateTime) & ":" & (Second(getDateTime))
End Function
感谢@Lankymart 指出我忽略的一个简单事实。请随意 post,我会接受你的答案。
在 another question 中处理了这个问题。
VBScript Date/Time 当您看到一种特定格式时,它被存储为一个整数,该格式只是运行时推断该 Date/Time 变量 的默认字符串表示(通常使用计算机区域设置).
Function FormatDate(df)
FormatDate = Right("00" & Day(df), 2) & _
"/" & Right("00" & Month(df), 2) & "/" & Year(df) & _
" " & Right("00" & Hour(df), 2) & ":" & _
Right("00" & Minute(df), 2) & ":" & Right("00" & Second(df), 2)
End Function
当您有日期 like 04/04/2015
例如 (只要 DBMS 支持)时,您还可以使用 Left(MonthName(Month(df)), 3)
来避免日期格式问题.