5

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 ?

casperOne
  • 72,334
  • 18
  • 180
  • 242
Jay
  • 1,365
  • 1
  • 17
  • 29

4 Answers4

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
3

Take a look at this post - it might help you out... How to transform a date-string in classic asp

Community
  • 1
  • 1
Luke
  • 422
  • 5
  • 15
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