none
Come up with a Startdate in the week and a end Date in the Week RRS feed

  • Question

  • I am trying to create two variables that will be passed to a command that needs a start date and an end date.

    THis is to gather date for transaction logs that are kept for 30 days.  So I figure that I need to run the script twice a month.  Once to grab the first two weeks of the month we are in.  And a second one that will grab the last two weeks of the month prior.  THe dates cannot include weekends.

    So for instance 1st of May is a Sunday so I need the start date to be 2nd of May and then enumerate the end date to be the friday after that.

    With a second start date to be the following Monday and end date the following friday.

    I tried a few things like this.

    These get me the Month prior and the first day of that Month.

    $LASTDAYOFMONTH = ((Get-Date | ForEach-Object {Get-Date -Date ([datetime]"$($_.Month) 1, $($_.Year)")}).AddMilliseconds(-1))

    $FIRSTDAYOFMONTH=GET-DATE $LASTDAYOFMONTH -Day 1

    and then this function

    Function Get-FirstDay($day) {

     

    While ($day.dayofweek -eq "Sunday" -or $day.dayofweek -eq "Saturday"){

    Write-Host $Day

    $Day = $day.AddDays(1)

    }

    Return $day

    }

    $StartDay = Get-FirstDay $FIRSTDAYOFMONTH

    The problem is within the function it is not seeing the day of week for the format.  I am not sure if I am going about it right.



    Jeff C

    Wednesday, June 15, 2016 4:06 PM

Answers

  • In addition to jrv comments, I am wondering if you are trying too hard? DateTime objects contain dates.

    Example of how to get the last day of a month:


    PS C:\> ([DateTime] "1/2016").AddMonths(1).AddDays(-1)
    Sunday, January 31, 2016 12:00:00 AM
    

    This outputs a DateTime object, of course. If you need to know if that day is on a weekend:


    PS C:\> $lastDay = ([DateTime] "1/2016").AddMonths(1).AddDays(-1)
    PS C:\> $lastday.DayOfWeek
    Sunday
    PS C:\> ($lastDay.DayOfWeek -eq 'Sunday') -or ($lastDay.DayOfWeek -eq 'Saturday')
    True
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 15, 2016 5:01 PM
    Moderator

All replies

  • We are not sure what you are trying to do.

    Are you trying to get the firstday of the week? That would be Day 0.

    PS D:\scripts> [datetime]::Today.DayOfWeek -as [int]
    3
    
    PS D:\scripts> ([datetime]'Sunday, June 6, 1999').DayOfWeek -as [int]
    0
    


    \_(ツ)_/

    Wednesday, June 15, 2016 4:23 PM
  • In addition to jrv comments, I am wondering if you are trying too hard? DateTime objects contain dates.

    Example of how to get the last day of a month:


    PS C:\> ([DateTime] "1/2016").AddMonths(1).AddDays(-1)
    Sunday, January 31, 2016 12:00:00 AM
    

    This outputs a DateTime object, of course. If you need to know if that day is on a weekend:


    PS C:\> $lastDay = ([DateTime] "1/2016").AddMonths(1).AddDays(-1)
    PS C:\> $lastday.DayOfWeek
    Sunday
    PS C:\> ($lastDay.DayOfWeek -eq 'Sunday') -or ($lastDay.DayOfWeek -eq 'Saturday')
    True
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, June 15, 2016 5:01 PM
    Moderator
  • Ok let me try to explain better.  If I run my script on June 7th.  I need it to grab a start date of the first Monday after the 15th of May and then and Enddate of that following Friday.

    Then loop thru and find a second startdate of the Next Monday and next Friday.  and do that until it gets to the last day of the month.

    I need to run another script after the 20th of the month that will grab the 1st day of the Month and create Startday and End Day upto the 15th or the  friday of the week that the 15th resides in.


     


    Jeff C

    Thursday, June 16, 2016 12:52 PM
  • Ok let me try to explain better.  If I run my script on June 7th.  I need it to grab a start date of the first Monday after the 15th of May and then and Enddate of that following Friday.

    Then loop thru and find a second startdate of the Next Monday and next Friday.  and do that until it gets to the last day of the month.

    I need to run another script after the 20th of the month that will grab the 1st day of the Month and create Startday and End Day upto the 15th or the  friday of the week that the 15th resides in.


     


    Jeff C

    It is simple arithmetic.

    Monday is day 1.  Friday is day 5.  Get the first day of the month and add.

    PS D:\scripts> $day1=[int]([datetime]'7/1/2016').DayOfWeek
    PS D:\scripts> $needed=(7-$day1+1)
    PS D:\scripts> ([datetime]'7/1/2016').AddDays($needed)
    
    Monday, July 4, 2016 12:00:00 AM

    Now just keep adding days to get the next Friday.  It is AddDays(5+7)

    Simple arithmetic.


    \_(ツ)_/


    • Edited by jrv Thursday, June 16, 2016 1:07 PM
    Thursday, June 16, 2016 1:03 PM
  • Havent had a chance yet to put this into wrapper script and try it.  


    Jeff C

    Wednesday, June 22, 2016 2:03 PM