VBScript如何格式化日期? [英] VBScript How can I Format Date?
本文介绍了VBScript如何格式化日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望日期看起来像 MM-DD-YYYY 而不是 MM / DD / YYYY 。
I want the date to look like MM-DD-YYYY instead of 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 />")
如果要使用其他格式您将必须创建自己的功能并解析月,年,日等,并将它们放在首选格式中。
If you want to use another format you will have to create your own function and parse Month, Year, Day, etc and put them together in your preferred format.
Function myDateFormat(myDate)
d = WhatEver(Day(myDate))
m = WhatEver(Month(myDate))
y = Year(myDate)
myDateFormat= m & "-" & d & "-" & y
End Function
Function WhatEver(num)
If(Len(num)=1) Then
WhatEver="0"&num
Else
WhatEver=num
End If
End Function
编辑:如果值小于 10
,则将日期和月份格式化为 0n
。
edit: added function to format day and month as 0n
if value is less than 10
.
这篇关于VBScript如何格式化日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文