Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP formatting date

Tags:

asp-classic

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>
like image 900
user1060187 Avatar asked May 14 '12 08:05

user1060187


1 Answers

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
like image 108
Guido Gautier Avatar answered Sep 24 '22 01:09

Guido Gautier