Get first day of current and next month in VBSCRIPT

vbscript

I can find first day of current and next month using SQL Server 2008
Thare is my code:

   declare @date_begin date= DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
   declare @date_end date= CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@date_begin))-1),DATEADD(mm,1,@date_begin)),101)
   select @date_begin, @date_end

It return me:

    2013-12-01  2014-01-01

But now I need to get first day of current and next month using VBScript. Can somebody help me?

Best Answer

You can do same thing using DateSerial in VBScript.

From Remarks Section - DateSerial Function:

To specify a date, such as December 31, 1991, the range of numbers for each DateSerial argument should be in the accepted range for the unit; that is, 1–31 for days and 1–12 for months.

However, you can also specify relative dates for each argument using any numeric expression that represents some number of days, months, or years before or after a certain date.

So, all you need is specifying year, month and day values.

Function CustomDate(dtm)
    Dim y, m, d, h, n, s
        y = Year(dtm)
        m = Right("0" & Month(dtm), 2)
        d = Right("0" & Day(dtm), 2)
        h = Right("0" & Hour(dtm), 2)
        n = Right("0" & Minute(dtm), 2)
        s = Right("0" & Second(dtm), 2)
    CustomDate = y & "-" & m & "-" & d
    If h + n + s > 0 Then
        CustomDate = CustomDate & " "
        CustomDate = CustomDate & h & ":" & n & ":" & s
    End If
End Function

WScript.Echo "1st day of current month", CustomDate(DateSerial(Year(Date), Month(Date), 1))
WScript.Echo "1st day of next month", CustomDate(DateSerial(Year(Date), Month(Date) + 1, 1))
Related Topic