Link to home
Start Free TrialLog in
Avatar of tentavarious
tentavarious

asked on

finding first day and last day of current month vb.net

What is the easiest way to find the first and last day of the current month?
Avatar of Tertioptus
Tertioptus


       Dim d As System.DateTime = Now
        MsgBox(d.AddDays(d.DaysInMonth(d.Year, d.Month) - d.Day()).ToString("MM/dd/yyyy"))
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tentavarious

ASKER

Anyway you can get me the working days in  a month, basically subtract all sundays and saturdays
Tertioptus,

You will need to loop through all days and checking if the day is a weekend or not
ohhh, okay, thanks
Could you give me a small example?
tentavarious,

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dtmTest As Date = cDateUtil.GetBeginOfCurrentMonth(Date.Today)
        Dim intWorkableDays As Integer

        Do While dtmTest <= cDateUtil.GetEndOfCurrentMonth(Date.Today)
            If dtmTest.DayOfWeek <> DayOfWeek.Saturday AndAlso dtmTest.DayOfWeek <> DayOfWeek.Sunday Then
                intWorkableDays += 1
            End If
            dtmTest = dtmTest.AddDays(1)
        Loop
        MessageBox.Show("there are " + intWorkableDays.ToString + " workable days")
    End Sub
why not just use this: it works in T-SQL and can be slightly altered to for VB using the NOW().
select DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)
OR:   =DateAdd("d",1-DatePart("d",Today()),Today())