Time and Date Functions
- Home
- ASP
- Time and Date Functions
Display The Time in 12hr format:
The Code: <%= time %>
The Result: 5:00:13 PM
Display The Date/Time in 12hr format:
The Code: <%= now %>
The Result: 10/11/2024 5:00:13 PM
Display The Day, Date and Year:
The Code: <% Response.Write(FormatDateTime(Now(),1)
& " " )%>
The Result: Friday, October 11, 2024
Display Only The Date:
The Code: <% Response.Write(FormatDateTime(Now(),2)
& " " )%>
The Result: 10/11/2024
Display The Time in 12hr Format:
The Code: <% Response.Write(FormatDateTime(Now(),3)
& " " )%>
The Result: 5:00:13 PM
Display The Time in 24hr Format:
The Code: <% Response.Write(FormatDateTime(Now(),4)
& " " )%>
The Result: 17:00
Display The Name Of The Current Month:
The Code: <%=MonthName(Month(Date))%>
The Result: October
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: 11
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:
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: 10/11/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 10/11/2024, and in 100
days it will be 1/19/2025.
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 80 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:
Out of 60 minutes in each hour, the current minute
is 0.
Of 60 seconds in each minute Which Second Is It Now:
The Code:
Out of 60 seconds in each minute, the current
second is 13.
Which month is it In Numeric Format:
The Code:
Of the 12 months in each year, this is month # 10.
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 # 41 of the current year 2024!
Abbreviate The Month as Oct..
The Code:
<%
DisplayMonth = (MonthName(Month(Date)))
DisplayMonth = Left(DisplayMonth, 3) & "."
%>
Code to Display The Answer:
<% Response.Write(DisplayMonth)%>
The Result: Oct.
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: 10/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: 10/31/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 11/10/2024
Calculate Hours Past or Since:
The Code:
<%=DateDiff("h",
Now, "03/06/96 06:30:00")*-1%>
The psacake.com has been online 228899 hours.
Calculate Days Past:
The Code:
<%=DateDiff("d",
Now, "03/06/96 00:00:00")*-1%>
The psacake.com has been online 9537 days.
Calculate Weeks Past:
The Code:
<%=DateDiff("ww",
Now, "03/06/96 00:00:01")*-1%>
The psacake.com has been online 1362 weeks.
Calculate Weeks (or dates) In The Future:
The Code:
<%=DateDiff("ww",
Now, "01/01/06 00:00:01")*1%>
There are -562 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