Is there an API I could use to calculate the last day of any month?
Here's the code to load the UDF into SQL Server. The calc logic is the line that starts with "SET @LastDayOfMonth.." It returns the last day of the month for the date in @SelectedDate. I got the logic somewhere on the web. Probably no reason you can't translate it to whatever you're coding in.
USE [db name here] GO -- Drop the function if it's already in place to avoid error trying to add it again. IF ( OBJECT_ID('dbo.LAST_DAY') IS NOT NULL ) DROP FUNCTION [dbo].[LAST_DAY] GO /****** Object: UserDefinedFunction [dbo].[LAST_DAY] Script Date: 11/27/2012 8:18:36 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Mike Palandri -- Create date: 11/21/2012 -- Description: Returns last day of month in which passed date falls -- ============================================= CREATE FUNCTION [dbo].[LAST_DAY] ( -- Add the parameters for the function here @SelectedDate datetime ) RETURNS date AS BEGIN -- Declare the return variable here DECLARE @LastDayOfMonth datetime -- Add the T-SQL statements to compute the return value here SET @LastDayOfMonth = (SELECT CONVERT(char(10), DATEADD(s,-1, DATEADD(mm, DATEDIFF(m,0,@SelectedDate)+1,0)),101)) -- Return the result of the function RETURN @LastDayOfMonth END GO GRANT Execute ON [dbo].LAST_DAY TO PUBLIC GO
MOVE WHATEVER-DATE TO WSDR-FR-DATE. MOVE 01 TO WSDR-FR-DAY. MOVE 1 TO WSDR-MONTH-INCR. MOVE ZEROES TO WSDR-DAY-INCR. MOVE ZEROES TO WSDR-YEAR-INCR. MOVE "Y" TO WSDR-EOM-ROLLOVER. PERFORM 900-INCREMENT-DATE. MOVE WSDR-TO-DATE TO WSDR-FR-DATE. MOVE ZEROES TO WSDR-MONTH-INCR. MOVE -1 TO WSDR-DAY-INCR. MOVE ZEROES TO WSDR-YEAR-INCR. MOVE "Y" TO WSDR-EOM-ROLLOVER. PERFORM 900-INCREMENT-DATE. MOVE WSDR-TO-DATE TO WHATEVER-DATE.