Time and Date Functions

Display The Time in 12hr format:
The Code:   <%= time %>
The Result:   2:04:43 PM


Display The Date/Time in 12hr format:
The Code:   <%= now %>
The Result:   9/26/2017 2:04:43 PM

Display The Day, Date and Year:
The Code:   <% Response.Write(FormatDateTime(Now(),1) & " " )%>
The Result:   Tuesday, September 26, 2017

Display Only The Date:
The Code:   <% Response.Write(FormatDateTime(Now(),2) & " " )%>
The Result:   9/26/2017

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

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

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

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

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

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

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:
    September 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: 9/22/2017
  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 9/26/2017, and in 100 days it will be 1/4/2018.
  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 95 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 4.
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 43.
Which month is it In Numeric Format:
The Code:
    <%=Month(Date)%>
Of the 12 months in each year, this is month # 9.
Display the current Day Of The Week:
The Code:
    <%=WeekDayName(WeekDay(Date))%>
Of the 7 days in each week, today is Tuesday.
And just what Week Is It?
The Code:
    <% TimeVar = (DatePart("ww",date)) %>
Display The Answer:
    <% Response.Write(TimeVar) %>
This is week # 39 of the current year 2017!

Abbreviate The Month as Sep..
The Code:

    <%
    DisplayMonth = (MonthName(Month(Date)))
    DisplayMonth = Left(DisplayMonth, 3) & "."
    %>
Code to Display The Answer:
    <% Response.Write(DisplayMonth)%>
The Result:   Sep.
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:   9/1/2017
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:   9/30/2017
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 26/9/2017

Calculate Hours Past or Since:
The Code:

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

Calculate Days Past:
The Code:

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

Calculate Weeks Past:
The Code:

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

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

    <%=DateDiff("ww", Now, "01/01/06 00:00:01")*1%>
There are -195 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 - 2017 psacake.com | My3C's

Version 7.2 | Advertise on this site