ASP date function

xiaoxiao2021-03-29  193

Author: Hmily Date: 2006-03-21

code

<% = Year (now)%> Year <% = Month (now)%> Yue <% = day (now)%> Japan <% = weekdayName (WeekDay (now))%>

display:

Today is: Friday, October 31, 2003

VBScript has many functions that allow you to get the date and time of various formats. You have used one of these functions. You can use the function now to return the current date and time:

code

At the Tone, The Time Will Bi: <% = now%>

What you should pay attention to, the return date and time is the date and time of the system clock of your web server. If someone in New York is watching your web, he sees the date and time of the local and the local date and time may be different.

The function now returns the date and time. If you just want to return to the current date, you can use the function date. If you just want to return to the current time, you can use the function time. E.g:

code

The date is: <% = DATE%>

The time is: <% = Time%>

Operation date

Use the function month (), day (), weekday (), and year (), you can split a date into a smaller part. All of these functions use a date expression as a parameter and return a number. Here is an example of how to use these functions:

code

THE MONTH IS: <% = Month (Date)%>

THE date is: <% = day (date)%>

The weekday is: <% = weekday (date)%>

The year is: <% = year (date)%>

Suppose the current date is August 9, 1997, Wednesday. If you include the above example in an ASP page, you will display the following text in your browser:

THE MONTH IS: 7

THE date is: 9

THE Weekday IS: 4

The year is: 1997

Note that weekday () assumes that the first day of a week is Sunday. If you want to use Monday as the first day of the week, you can use the following statement:

code

The weekday is: <% = weekday (date, vbwednesday)%>

You can use any day as the first day of the week. To specify the first day of the week is the day of the week, just replace the second parameter of the function weekday () with VBSunday, Vbmonday, VBTHURDAY, VBWEDsDAY, VBTHURDAY, VBFRIDAY, or VBSATURDAY.

You can not only use the function Date as the parameters of these functions. You can also provide a date string or a date constant as a parameter, as shown in the following example:

code

The weekday is: <% = weekday (# 12/25/2000 #)%>

THE weekday is: <% = weekday ("12-25-2000")%>

Both functions return 2000 Christmas this day is the day. (This may make you not used to: Return value is 2, representing Monday.) Expression # 12/25/2000 # is a date constant, the date constant is always included in the character '#'. Expression "12-25-2000" is a date string. It is feasible to use these two ways to provide a function.

To return to the month and the day of the day, you can use the function weekdayName () or MonthName (). These two functions returned to strings. Here is an example of using these two functions: program code

THE MONTH IS: <% = MONTHNAME (Month)%>

The weekday is: <% = weekdayName (Weekday (Date))%>

Suppose the current date is August, Wednesday. In this case, the first function returns a string july, the second function returns a string Wednesday. You can use any integers between 1 to 12 as a parameter of the function MONTHNAME (), you can use any integers between 1 and 7 as the parameters of the function weekdayName ().

By default, these two functions do not abbreviate the returned string. The function MONTHNAME () returns July instead of JUL, the function weekdayName () returns Wednesday instead of the WED. However, by specifying the second parameter as True, you can force these two functions to return an abbreviated string. (True means that you want abbreviations, false means no abbreviation.) As shown in the following example:

code

THE MONTH IS: <% = MONTHNAME (Month (Date), True)%>

The weekday is: <% = weekdayName (Weekday (Date), True)%>

Operating time

You can also divide the time into a smaller part. You can return different parts of the time through the function hours (), Minute (), and Second (). Below is some examples of these functions and possible return values:

code

The Hour is: <% = Hour (Time)%>

The Hour is: 21

The Minute IS: <% = Minute (Time)%>

The Minute IS: 39

The second is: <% = second (time)%>

The Second IS: 34

The function hour () returns an integer between 0 and 23 (0 points is one hour after midnight). The function minute () returns an integer between 0 and 59. The function second () also returns an integer between 0 and 59.

You can use the function time as the parameters of these functions, you can also provide a time constant or time string as a parameter. The following two examples are extracted from time to 34:

code

The minute is: <% = minute (# 12: 34: 19 #%>

The Minute IS: 34

The minute is: <% = minute ("12:34:23")%>

The Minute IS: 34

Compare date and time

VBScript has two functions for comparing the date and time. You can use the function dateadd () to add the date and time to calculate the interval between two dates or times with the function dateDiff (). Below is some examples of using the function dateAdd ():

code

Your Registration Will EXPRIE ON <% = DateAdd ("WW", 6, DATE)%>

Exactly Fifteen Seconds from Now, At <% = DateAdd ("S", 15, TIME)%> Your Computer

Will Melt.

The first example returns the date than the date of the current date, the second example returns the number of seconds in 15 seconds.

Function dateadd () has three parameters: the first parameter specifies a time interval. (See Table 8.2)

The second parameter time interval multiple factor.

Finally, the third parameter is a variable or constant of a date or time.

Table 8.2 Date and interval

Interval

code

YYYY year

Q quarter

M month

Y

D days

W days

WW week

H hours

M minutes

s second

You can use a function dateDiff to determine the interval between two dates or times. The following example demonstrates how to use this function:

code

You Have Been a Member for <% = Datediff ("D", "1/1/1988", Date)%> DAYS.

There is exactly <% = datediff ("s", date, "1/1/2000")%> Seconds Remaining Until

The year 2000.

The dateDiff () function in the first example returns the number of days between 1/1/1988 and the current date. Datediff () in the second example returns the number of seconds between the current date to 2000.

Function datediff () has three parameters:

The first parameter is a date or time interval (see Table 8.2).

The other two parameters are two dates. In order to avoid negative numbers, the first date parameter should be better than the second. (If the function returns a negative number, you should know that the first date is late than the second date.)

Format date and time

You can specify a date or time display format. By default, when you display a date through the function date, it looks like this:

code

7/9/97

However, you can use a function FORMATDATETIME () to display a date based on the area settings of your computer. You can display a date as a short date format or long date format.

note

Select a zone setting icon from the Control Panel, you can set the area setting for your computer. You can specify a long format or short format of the date and time via zone settings.

The following example demonstrates how to control the display of long-term formats and short date format:

code

Short Date: <% = formatdatetime (date, vbshortdate)%>

Long Date: <% = formatdatetime (date, vblongdate)%>

When the date is set according to the UK (US) area, the date is displayed as the following format:

Short date: 7/9/97

Long Date: Wednesday, July 09,1997

Note that the display of the short date format is exactly the same as if you do not format. By default, the date is displayed in a short date format.

You can also format a function formatDateTime (). Similarly, you can specify a time display as a long format or short format. When using a short format display time, a 24-hour clock (military time) will be used. The following example demonstrates how to use the formatdatetime () function to operate the time:

code

Short time: <% = formatdatetime (time, vbshorttime)%>

Long time: <% = formatdatetime (time, vblongtime)%>

When the display time is set in the UK (US) area, the time format is as follows:

Short Time: 03: 20

Long Time: 3: 20: 08 AM

转载请注明原文地址:https://www.9cbs.com/read-130481.html

New Post(0)