//  home   //  advanced search   //  news   //  categories   //  sql build chart   //  downloads   //  statistics
 ASP FAQ 
Home
ASP FAQ Tutorials

   8000XXXX Errors
   Alerts
   ASP.NET 2.0
   Classic ASP 1.0
      COM / ActiveX Components
      Forms
      General Topics
      Date/Time Routines
      Email Scripts & Info
      Files/Directories & FSO
   Databases
   General Concepts
   Search Engine Optimization (SEO)

Contact Us
Site Map

Search

Web
aspfaq.com
tutorials.aspfaq.com
classicasp.aspfaq.com

ASP FAQ Tutorials :: Classic ASP 1.0 :: Date/Time Routines :: How do I calculate dates, such as the first day of the month?


How do I calculate dates, such as the first day of the month?

For SQL Server, run this script and you will see some of the tricks I've been known to use. Most monthly / annual calculations can be determined from two simple calculations: the first day of this month, and the first day of this year. From there, it's just a matter of adding months/years and, in cases of last day of month/year, subtracting 1. 
 
DECLARE 
    @today SMALLDATETIME, 
    @fdly SMALLDATETIME, -- first day of last year 
    @ldly SMALLDATETIME, -- last day of last year 
    @fdty SMALLDATETIME, -- first day of this year 
    @ldty SMALLDATETIME, -- last day of this year 
    @fdny SMALLDATETIME, -- first day of next year 
    @ldny SMALLDATETIME, -- last day of next year 
    @fdlm SMALLDATETIME, -- first day of last month 
    @ldlm SMALLDATETIME, -- last day of last month 
    @fdtm SMALLDATETIME, -- first day of this month 
    @ldtm SMALLDATETIME, -- last day of this month 
    @fdnm SMALLDATETIME, -- first day of next month 
    @ldnm SMALLDATETIME, -- last day of next month 
    @refd SMALLDATETIME, -- @today + 1 - @thisDay 
            -- (actually first day of this month) 
    @thisDay TINYINT,     -- DAY(@today) 
    @refm SMALLINT,     -- 1 - @thismonth 
    @numdlm TINYINT,     -- number of days, last month 
    @numdtm TINYINT,     -- number of days, this month 
    @numdnm TINYINT     -- number of days, next month 
 
PRINT CHAR(13)+CHAR(10)+'today:' 
SET @today = DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE())) 
PRINT @today 
PRINT '('+DATENAME(WEEKDAY, @today)+')' 
 
SELECT 
    @thisDay = DAY(@today), 
    @refd = @today + 1 - DAY(@today), 
    @refm = 1 - MONTH(@today) 
 
PRINT CHAR(13)+CHAR(10)+'first day of last year:' 
SET @fdly = DATEADD(YEAR, -1, DATEADD(MONTH, @refm, @refd)) 
PRINT @fdly 
PRINT '('+DATENAME(WEEKDAY, @fdly)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of last year:' 
SET @ldly = DATEADD(MONTH, @refm, @refd) - 1 
PRINT @ldly 
PRINT '('+DATENAME(WEEKDAY, @ldly)+')' 
 
PRINT CHAR(13)+CHAR(10)+'first day of this year:'  
SET @fdty = DATEADD(MONTH, @refm, @refd) 
PRINT @fdty 
PRINT '('+DATENAME(WEEKDAY, @fdty)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of this year:' 
SET @ldty = DATEADD(YEAR, 1, DATEADD(MONTH, @refm, @refd)) - 1 
PRINT @ldty 
PRINT '('+DATENAME(WEEKDAY, @ldty)+')' 
 
PRINT CHAR(13)+CHAR(10)+'first day of next year:' 
SET @fdny = DATEADD(YEAR, 1, DATEADD(MONTH, @refm, @refd)) 
PRINT @fdny 
PRINT '('+DATENAME(WEEKDAY, @fdny)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of next year:' 
SET @ldny = DATEADD(YEAR, 2, DATEADD(MONTH, @refm, @refd)) - 1 
PRINT @ldny 
PRINT '('+DATENAME(WEEKDAY, @ldny)+')' 
 
PRINT CHAR(13)+CHAR(10)+'first day of last month:' 
SET @fdlm = DATEADD(MONTH, -1, @refd) 
PRINT @fdlm 
PRINT '('+DATENAME(WEEKDAY, @fdlm)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of last month:' 
SET @ldlm = @today - @thisDay 
PRINT @ldlm 
PRINT '('+DATENAME(WEEKDAY, @ldlm)+')' 
 
-- little shortcut here... number of days in a month is 
-- equal to the calendar day of the last day of the month 
 
PRINT CHAR(13)+CHAR(10)+'number of days in last month:' 
SET @numdlm = DAY(@today - @thisDay) 
PRINT @numdlm 
 
PRINT CHAR(13)+CHAR(10)+'first day of this month:' 
SET @fdtm = @refd 
PRINT @fdtm 
PRINT '('+DATENAME(WEEKDAY, @fdtm)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of this month:' 
SET @ldtm = DATEADD(MONTH, 1, @refd) - 1 
PRINT @ldtm 
PRINT '('+DATENAME(WEEKDAY, @ldtm)+')' 
 
PRINT CHAR(13)+CHAR(10)+'number of days in this month:' 
SET @numdtm = DAY(DATEADD(MONTH, 1, @refd) - 1) 
PRINT @numdtm 
 
PRINT CHAR(13)+CHAR(10)+'first day of next month:' 
SET @fdnm = DATEADD(MONTH, 1, @refd) 
PRINT @fdnm 
PRINT '('+DATENAME(WEEKDAY, @fdnm)+')' 
 
PRINT CHAR(13)+CHAR(10)+'last day of next month:' 
SET @ldnm = DATEADD(MONTH, 2, @refd) - 1 
PRINT @ldnm 
PRINT '('+DATENAME(WEEKDAY, @ldnm)+')' 
 
PRINT CHAR(13)+CHAR(10)+'number of days in next month:' 
SET @numdnm = DAY(DATEADD(MONTH, 2, @refd) - 1) 
PRINT @numdnm
 
And here it is in VBScript: 
 
<% 
    today = Date() 
 
    thisMonth = month(today) 
    thisDay = day(today) 
    refd = today + 1 - thisDay 
 
    response.write "Today:<br>" 
    response.write today & "<br>" 
    response.write "(" & fdate(today) & ")" 
 
    response.write "<p>First day of last year:<br>" 
    fdly = dateadd("yyyy", -1, dateadd("m", 1-thisMonth, refd))  
    response.write fdly & "<br>" 
    response.write "(" & fdate(fdly) & ")" 
 
    response.write "<p>Last day of last year:<br>" 
    ldly = dateadd("m", 1-thisMonth, refd) - 1 
    response.write ldly & "<br>" 
    response.write "(" & fdate(ldly) & ")" 
 
    response.write "<p>First day of this year:<br>" 
    fdty = dateadd("m", 1-thisMonth, refd) 
    response.write fdty & "<br>" 
    response.write "(" & fdate(fdty) & ")" 
 
    response.write "<p>Last day of this year:<br>" 
    ldty = dateadd("yyyy", 1, dateadd("m", 1-thisMonth, refd)) - 1 
    response.write ldty & "<br>" 
    response.write "(" & fdate(ldty) & ")" 
 
    response.write "<p>First day of next year:<br>" 
    fdny = dateadd("yyyy", 1, dateadd("m", 1-thisMonth, refd)) 
    response.write fdny & "<br>" 
    response.write "(" & fdate(fdny) & ")" 
 
    response.write "<p>Last day of next year:<br>" 
    ldny = dateadd("yyyy", 2, dateadd("m", 1-thisMonth, refd)) - 1 
    response.write ldny & "<br>" 
    response.write "(" & fdate(ldny) & ")" 
 
    response.write "<p>First day of last month:<br>" 
    fdlm = dateadd("m", -1, refd) 
    response.write fdlm & "<br>" 
    response.write "(" & fdate(fdlm) & ")" 
 
    response.write "<p>Last day of last month:<br>" 
    ldlm = refd - 1 
    response.write ldlm & "<br>" 
    response.write "(" & fdate(ldlm) & ")" 
 
    response.write "<p>Number of days in last month:<br>" 
    numdlm = day(refd - 1) 
    response.write numdlm & "<br>" 
 
    response.write "<p>First day of this month:<br>" 
    fdtm = refd 
    response.write fdtm & "<br>" 
    response.write "(" & fdate(ldny) & ")" 
 
    response.write "<p>Last day of this month:<br>" 
    ldtm = dateadd("m", 1, refd) - 1 
    response.write ldtm & "<br>" 
    response.write "(" & fdate(ldtm) & ")" 
 
    response.write "<p>Number of days in this month:<br>" 
    numdtm = day(dateadd("m", 1, refd) - 1) 
    response.write numdtm & "<br>" 
 
    response.write "<p>First day of next month:<br>" 
    fdnm = dateadd("m", 1, refd) 
    response.write fdnm & "<br>" 
    response.write "(" & fdate(fdnm) & ")" 
 
    response.write "<p>Last day of next month:<br>" 
    ldnm = dateadd("m", 2, refd) - 1 
    response.write ldnm & "<br>" 
    response.write "(" & fdate(ldnm) & ")" 
 
    response.write "<p>Number of days in next month:<br>" 
    numdnm = day(dateadd("m", 2, refd) - 1) 
    response.write numdnm & "<br>" 
 
    function fdate(dt) 
        fdate = formatdatetime(dt, 1) 
    end function 
%>
 
And finally, in JavaScript, thanks to Chris Hohmann: 
 
<script language="JavaScript" runat="SERVER"> 
 
var today = new Date(); 
var thisYear = today.getYear(); 
var thisMonth = today.getMonth(); 
var thisDay = today.getDate(); 
 
//more laziness: 
 
function rw(s1, s2) 

    Response.Write("<tr><td>"+s1+"</td><td>"+s2+"</td></tr>"); 

 
Response.Write("<table border='1'>"); 
 
rw("Today:", today.toDateString()); 
 
//Years 
var fdly = new Date(thisYear - 1, 0, 1); 
rw("First day of last year:", fdly.toDateString()); 
 
var ldly = new Date(thisYear, 0, 0); 
rw("Last day of last year:", ldly.toDateString()); 
 
var fdty = new Date(thisYear, 0, 1); 
rw("First day of this year:", fdty.toDateString()); 
 
var ldty = new Date(thisYear + 1, 0, 0); 
rw("Last day of this year:", ldty.toDateString()); 
 
var fdny = new Date(thisYear + 1, 0 ,1); 
rw("First day of next year:", fdny.toDateString()); 
 
var ldny = new Date(thisYear + 2, 0, 0); 
rw("Last day of next year:", ldny.toDateString()); 
 
//Months 
var fdlm = new Date(thisYear, thisMonth - 1 ,1); 
rw("First day of last month:", fdlm.toDateString()); 
 
var ldlm = new Date(thisYear, thisMonth, 0); 
rw("Last day of last month:", ldlm.toDateString()); 
 
rw("Number of days in last month:", ldlm.getDate()); 
 
var fdtm = new Date(thisYear, thisMonth, 1); 
rw("First day of this month:", fdtm.toDateString()); 
 
var ldtm = new Date(thisYear, thisMonth + 1, 0); 
rw("Last day of this month:", ldtm.toDateString()); 
 
rw("Number of days in this month:", ldtm.getDate()); 
 
var fdnm = new Date(thisYear, thisMonth + 1, 1); 
rw("First day of next month:", fdnm.toDateString()); 
 
var ldnm = new Date(thisYear, thisMonth + 2, 0); 
rw("Last day of next month:", ldnm.toDateString()); 
 
rw("Number of days in next month:", ldnm.getDate()); 
 
Response.Write("</table>"); 
 
</script>
 
As always, please send us feedback if you have any suggestions for date ranges not handled above, or more efficient ways to handle these calculations.

Related Articles

Can I get millisecond accuracy in ASP?
Can I make VBScript format dates for me?
Could I get a little help with dates?
Given a date, how do I find the beginning and end of that week?
Given two dates, how do I determine an age?
How do I convert a DATEDIFF to days, hours, and minutes?
How do I convert a timespan, in seconds, to HH:MM:SS?
How do I convert local time to UTC (GMT) time?
How do I count the number of business days between two dates?
How do I delimit/format dates for database entry?
How do I determine the number of seconds since 1/1/1970?
How do I display time in military format?
How do I implement a calendar / datepicker in ASP?
How do I select time only from a DATETIME column?
Why do I have problems inserting NOW() into a database?
Why does JavaScript's document.lastModified() not work in ASP files?

 

 


Created: 2/3/2003 | Last Updated: 3/10/2005 | broken links | helpful | not helpful | statistics
© Copyright 2006, UBR, Inc. All Rights Reserved. (368)

 

Copyright 1999-2006, All rights reserved.
Finding content
Finding content.  An error has occured...