none
Find date on second Tuesday of every month

    Question

  • I am trying to find out date on every second Tuesday of every months using powershell so I can control my monitoring scripts. Any help? Our servers are patched on Sunday after every second Tuesday once patches are relased. If I know the date then I can stop my monitoring scripts during that period since servers are down for patching. 

    Monday, March 19, 2012 11:07 PM

Answers

  • Turned out it is easier than I thought...

    $FindNthDay=2

    $WeekDay='Tuesday'

    [datetime]$Today=[datetime]::NOW

    $todayM=$Today.Month.ToString()

    $todayY=$Today.Year.ToString()

    [datetime]$StrtMonth=$todayM+'/1/'+$todayY

    while ($StrtMonth.DayofWeek -ine $WeekDay ) { $StrtMonth=$StrtMonth.AddDays(1) }

    $StrtMonth.AddDays(7*($FindNthDay-1))

    Tuesday, March 20, 2012 12:22 AM

All replies

  • Turned out it is easier than I thought...

    $FindNthDay=2

    $WeekDay='Tuesday'

    [datetime]$Today=[datetime]::NOW

    $todayM=$Today.Month.ToString()

    $todayY=$Today.Year.ToString()

    [datetime]$StrtMonth=$todayM+'/1/'+$todayY

    while ($StrtMonth.DayofWeek -ine $WeekDay ) { $StrtMonth=$StrtMonth.AddDays(1) }

    $StrtMonth.AddDays(7*($FindNthDay-1))

    Tuesday, March 20, 2012 12:22 AM
  • Here's how I solved this:

    #Returns a date object of the second tuesday of the month for given moth and year
    function SecondTuesday ([int]$Month, [int]$Year) {
        [int]$Day = 1
        while((Get-Date -Day $Day -Hour 0 -Millisecond 0 -Minute 0 -Month $Month -Year $Year -Second 0).DayOfWeek -ne "Tuesday") {
            $day++
        }
        $day += 7
        return (Get-Date -Day $Day -Hour 0 -Millisecond 0 -Minute 0 -Month $Month -Year $Year -Second 0)
    }

    Then I just drop this function into any scripts where I might need it. I know you solved this, but since this is the first hit on Google I thought it'd be a good place to drop 2 a second solution so that it might help someone else.


    -Tom

    Thursday, February 07, 2013 9:26 PM
  • I am needing to change the output of the script you created to YYYY-MM-DD and I just cannot figure it out. Any help would be greatly appreciated.
    Sunday, December 04, 2016 4:16 PM
  • You can try it like this, this does what you ask. i use the script to send whatsapp messages to my engineers with whatsapp on certain days of the month.

    -----------------------------------

    function isdayofmonth ([int]$Month, [int]$Year, $weekday, $firstsecondthirdorfourthdayofmonth) {

        $nameofmonth=(Get-Culture).DateTimeFormat.GetMonthName($month)
        [int]$Day = 1
        while((Get-Date -Day $Day -Hour 0 -Millisecond 0 -Minute 0 -Month $Month -Year $Year -Second 0).DayOfWeek -ne $weekday) {
            $day++
        }
        if ($firstsecondthirdorfourthdayofmonth -eq 2) {$day += 7}
        if ($firstsecondthirdorfourthdayofmonth -eq 3) {$day += 14}
        if ($firstsecondthirdorfourthdayofmonth -eq 4) {$day += 21}

        return (Get-Date -Day $Day -Hour 0 -Millisecond 0 -Minute 0 -Month $Month -Year $Year -Second 0 -format D)
    }

    #----------------------------------------------------------------------------------------------------------------------------------

    $datum=get-date
    if ((isdayofmonth $datum.month $datum.year tuesday 2) -eq (get-date -format D)) { do_something }

    Tuesday, January 10, 2017 4:09 PM