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
  • 73,706
  • 19
  • 184
  • 253
Jay
  • 1,384
  • 1
  • 17
  • 30

5 Answers5

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
  • 21,552
  • 13
  • 72
  • 102
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
  • Glad it helped... please mark as the answer if you wouldn't mind :-) – Luke Jan 17 '11 at 20:35
  • One thing to mention is, if you aren't just using this as a disposable display variable, and actually need to keep this information by inserting it into a database, the best rule of practice is to pass a NULL value to the database and have that column set to use a default expression, normally people use CURRENT_TIMESTAMP for example, but you want a different time than right now, so you just format that on the MySQL end so you dont have to deal with converting it. The only reason not to do it, is if you do not have access to the database or ability to do it, but if you can, I highly recommend. – easleyfixed Oct 05 '22 at 18:05
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,384
  • 1
  • 17
  • 30
  • 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
  • 450
  • 1
  • 7
  • 18
0

Build your YYYY-MM-DD using this routine:

strMonth = Month(Date)
strDay = Day(Date)

if len(strMonth) < 2 then
    strMonth = "0" & strMonth
end if

if len(strDay) < 2 then
    strDay = "0" & strDay
end if

strDate = Year(Date) & "-" & strMonth & "-" & strDay
WilliamK
  • 821
  • 1
  • 13
  • 32