Time and Date Functions
- Home
- ASP
- Time and Date Functions
Display The Time in 12hr format:
The Code: <%= time %>
The Result: 8:01:42 AM
Display The Date/Time in 12hr format:
The Code: <%= now %>
The Result: 12/21/2024 8:01:42 AM
Display The Day, Date and Year:
The Code: <% Response.Write(FormatDateTime(Now(),1)
& " " )%>
The Result: Saturday, December 21, 2024
Display Only The Date:
The Code: <% Response.Write(FormatDateTime(Now(),2)
& " " )%>
The Result: 12/21/2024
Display The Time in 12hr Format:
The Code: <% Response.Write(FormatDateTime(Now(),3)
& " " )%>
The Result: 8:01:42 AM
Display The Time in 24hr Format:
The Code: <% Response.Write(FormatDateTime(Now(),4)
& " " )%>
The Result: 08:01
Display The Name Of The Current Month:
The Code: <%=MonthName(Month(Date))%>
The Result: December
Display The Week Day Number Of The Current Day:
The Code: <%=WeekDay(Date)%>
The Result: 7
Display The Calendar Date Of The Current Day:
The Code: <%=Day(Date)%>
The Result: 21
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: 12/20/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 12/21/2024, and in 100
days it will be 3/31/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 9 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 1.
Of 60 seconds in each minute Which Second Is It Now:
The Code:
Out of 60 seconds in each minute, the current
second is 42.
Which month is it In Numeric Format:
The Code:
Of the 12 months in each year, this is month # 12.
Display the current Day Of The Week:
The Code:
<%=WeekDayName(WeekDay(Date))%>
Of the 7 days in each week, today is Saturday.
And just what Week Is It?
The Code:
<% TimeVar = (DatePart("ww",date)) %>
Display The Answer:
<% Response.Write(TimeVar) %>
This is week # 51 of the current year 2024!
Abbreviate The Month as Dec..
The Code:
<%
DisplayMonth = (MonthName(Month(Date)))
DisplayMonth = Left(DisplayMonth, 3) & "."
%>
Code to Display The Answer:
<% Response.Write(DisplayMonth)%>
The Result: Dec.
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: 12/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: 12/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 21/12/2024
Calculate Hours Past or Since:
The Code:
<%=DateDiff("h",
Now, "03/06/96 06:30:00")*-1%>
The psacake.com has been online 230594 hours.
Calculate Days Past:
The Code:
<%=DateDiff("d",
Now, "03/06/96 00:00:00")*-1%>
The psacake.com has been online 9608 days.
Calculate Weeks Past:
The Code:
<%=DateDiff("ww",
Now, "03/06/96 00:00:01")*-1%>
The psacake.com has been online 1372 weeks.
Calculate Weeks (or dates) In The Future:
The Code:
<%=DateDiff("ww",
Now, "01/01/06 00:00:01")*1%>
There are -572 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