Tuesday, September 8, 2015

Getting first and last day of previous month


[code]
    Private Sub CalculateDefaultDates()
        Dim wrkDate As Date
        Dim baseDate As Date
        baseDate = Now

        'get first of the last month
        wrkDate = baseDate.AddMonths(-1)

        wrkDate = New DateTime(wrkDate.Year, wrkDate.Month, 1)
        Dim sDate As String
        sDate = wrkDate.ToShortDateString()
        txtStartDate.Text = sDate

        'get the last day of the previous month
        wrkDate = baseDate
        wrkDate = New DateTime(wrkDate.Year, wrkDate.Month, 1)

        wrkDate = wrkDate.AddDays(-1)

        sDate = wrkDate.ToShortDateString()
        txtEndDate.Text = sDate


    End Sub
[/code]


another example:

firstDay = new DateTime(Today.Year, Today.Month, 1);
//first day of next month minus one day
lastDay = firstDay.AddMonths(1).AddDays(-1);
//See: http://msdn.microsoft.com/en-us/library/system.datetime.addmonths.aspx

No comments:

Post a Comment