Calculation of days left for a date

In ASP we make a page that calculates the days remaining for a date and says something like -There are 38 days left for day D-

We are going to build a script that calculates the days remaining to reach any date. So that the web page can inform the user by saying something like “My birthday is 28 days away”.

We could use this script on a web page for an event, fair or any other event. It’s old fashioned to give this example, but we could have made a page that said “Year 2000 is 280 days away”

Since we are working with server scripts, the date on which the remaining days will be calculated will be the server system date. This has the drawback that the server’s time may be slightly different from the client’s computer, if the two systems are in countries with different time slots. We could do a similar script in client Javascript and it would take the date from the client, which can be useful in some cases. We simply point it out here because it is interesting for the reader to know.

We find out the dates

To perform our calculation we start by finding out the current date of the server and the date of the future event.

current_date = Now

future_date = CDate(“1/1/2025”)

In these two lines of code, we first get the current time by setting the current_date variable to the value of the Now system variable, which contains the server’s date.

Later we obtain the future date from a string of characters, converting it to a Date object (date) with the CDate() function. In the example we obtain the date corresponding to January 1, 2025, it could be any other.

See also  Introduction to the Internet

DateDiff function

There is a very useful Visual Basic Script function for the exercise. It is the DateDiff function, which calculates the difference between two dates and can do the calculation in days, hours, minutes, seconds, etc.

The syntax is as follows:

DateDiff(interval, date1, date2)

Where interval is the unit in which we want to do the calculation “s” for seconds, “d” for days, “h” for hours, “m” for months, “yyyy” for years…

The date1 and date2 parameters are the two dates involved in the subtraction.

We can access one from Microsoft.

We calculate the days

The use of the DateDiff function with the dates that we had previously obtained is very simple.

days_remaining = DateDiff(“d”, current_date, future_date)

With this we have already obtained the searched value and we could print it directly on the page.

All together

Finally we are going to see the whole exercise at once in an ASP page.





Calculation of days remaining for a date




<%
current_date = Now

future_date = CDate(“1/1/2025”)

days_remaining = DateDiff(“d”, current_date, future_date)

%>

There are <%=days_remaining%> days left for the year 2025



Can .

This is the end of this ASP workshop that can also be used to learn how to calculate other types of differences, for example the seconds remaining for a date, the months, or anything else. You would only have to use the DateDiff function, passing it another interval by parameter.

Loading Facebook Comments ...
Loading Disqus Comments ...