VBScript 如何格式化日期?

编程入门 行业动态 更新时间:2024-10-08 14:50:54
本文介绍了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 = 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

如果值小于10,添加了将日期和月份格式化为0n的功能.

edit: added function to format day and month as 0n if value is less than 10.

更多推荐

VBScript 如何格式化日期?

本文发布于:2023-05-28 00:48:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/308259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   VBScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!