Tuesday, September 8, 2009

Get last date of a month with SQL or Js( javascript )

I think SQL does a pretty good job with dates. Here is a very simple way to get the last day of any month. The example retreaves the last day of the current month.


SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))

Replace the GETDATE() with whatever date you need to find the last day of the month in.

We can write a simple function also in javascript to get last date of that month.

This function is useful for populating a client side last date operations

function daysInMonth(month,year)

{ var m = [31,28,31,30,31,30,31,31,30,31,30,31]; if (month != 2) return m[month - 1]; if (year%4 != 0) return m[1]; if (year%100 == 0 && year%400 != 0) return m[1]; return m[1] + 1; }

No comments:

Post a Comment