Time and Date Functions

Display The Time in 12hr format:
The Code:   <%= time %>
The Result:   9:01:13 PM


Display The Date/Time in 12hr format:
The Code:   <%= now %>
The Result:   4/19/2024 9:01:13 PM

Display The Day, Date and Year:
The Code:   <% Response.Write(FormatDateTime(Now(),1) & " " )%>
The Result:   Friday, April 19, 2024

Display Only The Date:
The Code:   <% Response.Write(FormatDateTime(Now(),2) & " " )%>
The Result:   4/19/2024

Display The Time in 12hr Format:
The Code:   <% Response.Write(FormatDateTime(Now(),3) & " " )%>
The Result:   9:01:13 PM

Display The Time in 24hr Format:
The Code:   <% Response.Write(FormatDateTime(Now(),4) & " " )%>
The Result:   21:01

Display The Name Of The Current Month:
The Code:   <%=MonthName(Month(Date))%>
The Result:   April

Display The Week Day Number Of The Current Day:
The Code:   <%=WeekDay(Date)%>
The Result:   6

Display The Calendar Date Of The Current Day:
The Code:   <%=Day(Date)%>
The Result:   19

Display The Current Year:
The Code:   <%=Year(Date)%>
The Result:   2024

Display # of Days In The Current Month:
The Code:
    <%
    DaysInMonth=Date
    CurrentDate=Day(DaysInMonth)
    BeginMonth=DateAdd("d",-(CurrentDate-1),DaysInMonth)
    EndMonth=(DateAdd("m",1, BeginMonth)-1)
    TotalDays=Day(EndMonth)
    %>

Code To Display The Answer:

    <%=Response.Write(TotalDays)%>
Display The Output:
    April Has # 30 Days.

Display The Date of a Previous Day, let's select last Friday:
The Code:
    <%
    dtmDate = CDate(FormatDateTime(Now,2))
    LastDay = dtmDate + 1 - (WeekDay(dtmDate, vbFriday))
    %>
Code To Display The Answer:
    <% Response.Write(LastDay)%>
Display The Output:
    On Last Friday, The Date Was: 4/19/2024
  You may change the day by altering the WeekDay statement, and changing the vbFriday to vbTuesday or the the day of your choice.  
Display The Date of a Previous or Future Date:
The Code:
    <%
    Dim FutureDate, TodaysDate
    FutureDate = FormatDateTime(Now(),2)
    TodaysDate = DateAdd("m", 0, FutureDate)
    FutureDate = DateAdd("d", +100, TodaysDate)
    %>
Code To Display The Answer(s):
    <%= TodaysDate %> - (Today's Date)
    <%= FutureDate %> - (Calculated Date)
Display The Output:
    Today is 4/19/2024, and in 100 days it will be 7/28/2024.
  We can also change this by editing two lines of code.  The number of days is calculated by changing the +100 in the fourth calculation line to a different number.  For example, entering a number like -200 will return a date 200 days prior to the current date.
Countdown To a Specific Date:
The Code:
    <%
    dim strDateTime
    strDateTime = Now()
    strFutureDay = #12/31#
    %>
Display The Result:
    <%= INT(strFutureDay - strDateTime) %>
The Result: There are 255 more days December 31st.

  To change the date, adjust the month and day parameters in the third line of code: #12/31# . Although easily modified to do so, this calculation does not count backward, or forward past the end of the current year.


Of 60 minutes in each hour Which Minute Is It Now:
The Code:
    <%=Minute(Time)%>
Out of 60 minutes in each hour, the current minute is 1.
Of 60 seconds in each minute Which Second Is It Now:
The Code:
    <%=Second(Time)%>
Out of 60 seconds in each minute, the current second is 13.
Which month is it In Numeric Format:
The Code:
    <%=Month(Date)%>
Of the 12 months in each year, this is month # 4.
Display the current Day Of The Week:
The Code:
    <%=WeekDayName(WeekDay(Date))%>
Of the 7 days in each week, today is Friday.
And just what Week Is It?
The Code:
    <% TimeVar = (DatePart("ww",date)) %>
Display The Answer:
    <% Response.Write(TimeVar) %>
This is week # 16 of the current year 2024!

Abbreviate The Month as Apr..
The Code:

    <%
    DisplayMonth = (MonthName(Month(Date)))
    DisplayMonth = Left(DisplayMonth, 3) & "."
    %>
Code to Display The Answer:
    <% Response.Write(DisplayMonth)%>
The Result:   Apr.
Calculate first date of current month
The Code:
    <%
    BaseDate = Date
    FirstOfMonth = DateSerial(Year(BaseDate), Month(BaseDate) + iOffset, 1)
    %>
Code to Display The Answer:
    <% Response.Write(FirstOfMonth)%>
The Result:   4/1/2024
Calculate last date of current month
The Code:
    <%
    BaseDate = date
    EndOfMonth = DateSerial(Year(BaseDate), Month(BaseDate) + 1, 0)
    %>
Code to Display The Answer:
    <% Response.Write(EndOfMonth)%>
The Result:   4/30/2024
How can I change this: 11/1/1999 to this: 1/11/1999 ?
The Code:
    <%
    Euro=date
    EuroDate =day(Euro) & "/" & month(Euro) & "/" & year(Euro)%>

    %>
Code to Display The Answer:
    <% Response.Write(EuroDate)%>
The Result:   The European formatted date is 19/4/2024

Calculate Hours Past or Since:
The Code:

    <%=DateDiff("h", Now, "03/06/96 06:30:00")*-1%>
The psacake.com has been online 224703 hours.

Calculate Days Past:
The Code:

    <%=DateDiff("d", Now, "03/06/96 00:00:00")*-1%>
The psacake.com has been online 9362 days.

Calculate Weeks Past:
The Code:

    <%=DateDiff("ww", Now, "03/06/96 00:00:01")*-1%>
The psacake.com has been online 1337 weeks.

Calculate Weeks (or dates) In The Future:
The Code:

    <%=DateDiff("ww", Now, "01/01/06 00:00:01")*1%>
There are -537 weeks remaining until January, 01, 2014.

The interval argument (above in bold burgandy) may have any of the following values:

yyyy Year q Quarter
m Month y Day of Year
d Day w Weekday
ww Week of Year h Hour
n Minute s Second



ASP Time date FormatDateTime(date format) formatDateTime


Back To Top
© 1998 - 2024 psacake.com
Version 7.21 | Advertise on this site