Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBScript How can I Format Date?

Tags:

date

vbscript

I want the date to look like MM-DD-YYYY instead of MM/DD/YYYY.

like image 432
Cocoa Dev Avatar asked Jan 25 '12 14:01

Cocoa Dev


People also ask

How do you format a date?

The international standard recommends writing the date as year, then month, then the day: YYYY-MM-DD.

How do I get today's date in VBScript?

How-to: Get the current date [getdate. vbs] Return the current Year/Month/Day and Time formatted as a string. GetDate.

Is date function in VBScript?

The IsDate function returns a Boolean value that indicates if the evaluated expression can be converted to a date. It returns True if the expression is a date or can be converted to a date; otherwise, it returns False.


1 Answers

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 

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

like image 59
stian.net Avatar answered Sep 28 '22 02:09

stian.net