ASP formatting date

2019-05-07 15:46发布

问题:

Hello there I'm trying to get a date in ASP to show up in a particular format (yyyymmdd). This is what I've tried so far but no luck. Any help is appreciated. Thanks

<tr>
    <td><b>Call Date</b></td>
    <% for i = -6 to 0 %>
        <td align=center>
            <a href="20a.asp?cldate=<% response.write(DateTime.Date()+i.ToString("yyyyMMdd")) %>" target="_blank">X</a>
        </td>
    <% Next %>
</tr>

回答1:

You can make use of the following functions:

Year(Now) '' Year in 4 digits
Month(Now) '' Month without leading zero
Day(Now) '' Day of the month without leading zero

DateAdd("d", <numofdays>, Now) '' add a number of days to your date

Read more about these (and other date functions) functions here.

If you need to add a leading zero:

function addLeadingZero(value)
    addLeadingZero = value
    if value < 10 then
        addLeadingZero = "0" & value
    end if
end function

An example of your case would be:

Dim today, myDate

today = Now

for i = -6 to 0
    myDate = DateAdd("d", i, today)

    response.write "<a href=""20a.asp?cldate=" & Year(myDate) & addLeadingZero(Month(myDate)) & addLeadingZero(Day(myDate)) & """ target=""_blank"">X</a>"
next


回答2:

Sorry to dig this up, but it might be of help to some people. Rather than the "If<10 then add leading zero" logic, I often use the right command and always add a leading zero...

response.write "<a href=""20a.asp?cldate=" & Year(myDate) & Right("0" & Month(myDate), 2) & right("0" & Day(myDate), 2) & """ target=""_blank"">X</a>"

..This way, you don't need a separate function, and it can be done on one line. I can't speak for the efficiency of it, but it seems logical.



回答3:

ASP gets the date from the OS not from the Database, a common error, but it is solved by use:

<%
' Date dd/mm/yyyy
Session.lcid=2057 '= UK English
%>

I hope it helps people.



回答4:

You can try. 100% tested!

   <% 
     mm = Month(now())
     dd = Day(now())
     yy = Year(now())
     IF len(mm) = 1 THEN
       mm = "0" & mm
     END IF
     IF len(dd) = 1 THEN
       dd = "0" & dd
     END IF
     response.write(yy & "/" & mm & "/" & dd) 
  %>


回答5:

 <%= DatePart("yyyy", Now) & "/" & DatePart("m", Now) & "/" & DatePart("d", Now) %>

Also refer

http://www.w3schools.com/vbscript/vbscript_ref_functions.asp

http://www.mikesdotnetting.com/Article/22/Date-formatting-in-VBScript

Thanks

Deepu



回答6:

Mid(date(), 7,4) & "-" & Mid(date(), 4,2) & "-" & Left(date(), 2)


标签: asp-classic