Im writing an small code and I need to add tomorrow's date on it. The format should be YYYY-MM-DD and I already tried " DateAdd("d",1,d_now) and it returns MM-DD-YYYY. How can I format this to YYYY-MM-DD ?
Asked
Active
Viewed 5.1k times
4 Answers
6
format is not enough....
'format a number with the correct amount of digits
eg: 9 becomes 09 but 11 stays 11'
Function formatNumber(value, digits)
if digits > len(value) then
formatNumber = String(digits-len(value),"0") & value
else
formatNumber = value
end if
End Function
'write the date "manually"'
response.write Year(date) & "-" & _
formatNumber(Month(date),2) & _
"-" & formatNumber(Day(date),2)
Caspar Kleijne
- 20,852
- 11
- 68
- 100
-
1Yes we are still using ASP Classic and this helped me out immensely. Thanks! – barvobot Aug 20 '21 at 23:43
1
I found the answer. This is the easiest way to do that.
d_now = date()
d = split(DateAdd("d",1,d_now),"/")
s = d(2) & "-" & d(0) & "-" & d(1)
Jay
- 1,365
- 1
- 17
- 29
-
One warning: if the system's "short date" format changes, this method might not work. That's because you're relying on VBScript's implicit conversion from Date to String (see http://msdn.microsoft.com/en-us/library/0zk841e9%28VS.85%29.aspx). If you're confident the "short date" setting won't change, then this is fine. Otherwise, it's more reliable to use the Year(), Month(), and Day() functions, like in Caspar's answer. – Cheran Shunmugavel Jan 18 '11 at 07:48
0
here is one more way to do it
function FormatMyDate(myDate)
FormatMyDate = Year(myDate) & "-" & _
Right("0" & Month(myDate), 2) & "-" & _
Right("0" & Day(myDate), 2)
end function
MeSo2
- 325
- 1
- 3
- 17