我希望日期的显示格式为MM-DD-YYYY,而非MM/DD/YYYY。
0 = vbGeneralDate - Default. Returns date: mm/dd/yy and time if specified: hh:mm:ss PM/AM.
1 = vbLongDate - Returns date: weekday, monthname, year
2 = vbShortDate - Returns date: mm/dd/yy
3 = vbLongTime - Returns time: hh:mm:ss PM/AM
4 = vbShortTime - Return time: hh:mm
d=CDate("2010-02-16 13:45")
document.write(FormatDateTime(d) & "<br />")
document.write(FormatDateTime(d,1) & "<br />")
document.write(FormatDateTime(d,2) & "<br />")
document.write(FormatDateTime(d,3) & "<br />")
document.write(FormatDateTime(d,4) & "<br />")
如果你想使用其他格式,你就需要创建自己的函数,解析月份、年份、日期等,并将它们按照你喜欢的格式组合在一起。Function myDateFormat(myDate)
d = TwoDigits(Day(myDate))
m = TwoDigits(Month(myDate))
y = Year(myDate)
myDateFormat= m & "-" & d & "-" & y
End Function
Function TwoDigits(num)
If(Len(num)=1) Then
TwoDigits="0"&num
Else
TwoDigits=num
End If
End Function
编辑:添加了一个函数,以0n
的格式格式化日期和月份(如果值小于10
)。
因此:
Function timeStamp()
Dim t
t = Now
timeStamp = Year(t) & "-" & _
Right("0" & Month(t),2) & "-" & _
Right("0" & Day(t),2) & "_" & _
Right("0" & Hour(t),2) & _
Right("0" & Minute(t),2) ' '& _ Right("0" & Second(t),2)
End Function
Right(Month(Now)+100,2)
- SafwanFormatDateTime的输出取决于控制面板中区域设置的配置。因此,在其他国家/地区,FormatDateTime(d, 2) 可能返回 yyyy-MM-dd 等格式。
如果您希望输出“文化不变”,请使用stian.net解决方案中的myDateFormat()。如果您只是不喜欢日期中的斜杠,并且不关心其他国家/地区的日期格式,您可以直接使用。
Replace(FormatDateTime(d,2),"/","-")
对于唯一的文件名,我使用
Dim ts, logfile, thisScript
thisScript = LEFT(Wscript.ScriptName,LEN(Wscript.ScriptName)-4) ' assuming .vbs extension
ts = timeStamp
logfile = thisScript & "_" & ts
' ======
Function timeStamp()
timeStamp = Year(Now) & "-" & _
Right("0" & Month(Now),2) & "-" & _
Right("0" & Day(Now),2) & "_" & _
Right("0" & Hour(Now),2) & _
Right("0" & Minute(Now),2) ' '& _ Right("0" & Second(Now),2)
End Function
' ======
right()
技巧来执行 rpad(x,2,"0")
。option explicit
Wscript.Echo "Today is " & myDate(now)
' date formatted as your request
Function myDate(dt)
dim d,m,y, sep
sep = "-"
' right(..) here works as rpad(x,2,"0")
d = right("0" & datePart("d",dt),2)
m = right("0" & datePart("m",dt),2)
y = datePart("yyyy",dt)
myDate= m & sep & d & sep & y
End Function
对于未来可能仍需要此信息的任何人。我的答案与qaweb非常相似,只是不那么令人生畏。在VBS中似乎没有很酷的自动简单函数来格式化日期。因此,您必须手动完成。我将日期的不同组成部分连接在一起。
Dim timeStamp
timeStamp = Month(Date)&"-"&Day(Date)&"-"&Year(Date)
run = msgbox(timeStamp)
11-22-2019
(根据当前日期而定)。虽然已经提供了答案,但我发现了更简单的解决方案:
日期:
01/20/2017
通过执行替换操作
CurrentDate = replace(date, "/", "-")
它将输出:
01-20-2017