none
Powershel. Date next Saturday RRS feed

  • Question

  • Hi. I need to get date next Saturday in the format dd-MM-yy

    I did so:

    1..1000 | ForEach-Object {
    $dte = (Get-Date).AddDays($_)
    
    if($dte.DayOfWeek -eq 'Saturday')
    { $dte }
    }
    
    $date = ($dte)[0]
    $date
      I get all the dates Saturdays, but I need variable only one date nearest Saturday in format:
    Get-date -Format dd-MM-yy

    Help please!

     
    Monday, February 2, 2015 10:47 AM

Answers

  • Hi Aleksandr,

    how about using less numbers?

    1..7 | ForEach-Object {
        $dte = (Get-Date).AddDays($_)
    
        if($dte.DayOfWeek -eq 'Saturday') { $dte.ToString("dd-MM-yy") }
    }


    Cheers,
    Fred

    Ps.: You can speed it up a bit more by doing this:

    foreach ($n in (0..6))
    {
    	$dte = [System.DateTime]::Now.AddDays($n)
    	
    	if ($dte.DayOfWeek -eq 'Saturday') { $dte.ToString("dd-MM-yy"); break }
    }

    Notice the difference?

    - the foreach statement is faster than the ForEach-Object Cmdlet (The cmdlet is easier on the memory if used properly, but that's not an issue here)
    - Using the .NET call to get the current Date is slightly faster than Get-Date
    - Using break stops the loop when finding the first Saturday
    - I changed the numbers-list to 0-6, down from 1-7. If today is Saturday, it'll return today (Don't know which way you need, so I thought I'd point that out - your version didn't check today).


    There's no place like 127.0.0.1


    • Edited by FWN Monday, February 2, 2015 11:57 AM
    • Marked as answer by Aleksandr Peresichan Thursday, February 5, 2015 6:57 PM
    Monday, February 2, 2015 11:49 AM

All replies

  • Hi Aleksandr,

    how about using less numbers?

    1..7 | ForEach-Object {
        $dte = (Get-Date).AddDays($_)
    
        if($dte.DayOfWeek -eq 'Saturday') { $dte.ToString("dd-MM-yy") }
    }


    Cheers,
    Fred

    Ps.: You can speed it up a bit more by doing this:

    foreach ($n in (0..6))
    {
    	$dte = [System.DateTime]::Now.AddDays($n)
    	
    	if ($dte.DayOfWeek -eq 'Saturday') { $dte.ToString("dd-MM-yy"); break }
    }

    Notice the difference?

    - the foreach statement is faster than the ForEach-Object Cmdlet (The cmdlet is easier on the memory if used properly, but that's not an issue here)
    - Using the .NET call to get the current Date is slightly faster than Get-Date
    - Using break stops the loop when finding the first Saturday
    - I changed the numbers-list to 0-6, down from 1-7. If today is Saturday, it'll return today (Don't know which way you need, so I thought I'd point that out - your version didn't check today).


    There's no place like 127.0.0.1


    • Edited by FWN Monday, February 2, 2015 11:57 AM
    • Marked as answer by Aleksandr Peresichan Thursday, February 5, 2015 6:57 PM
    Monday, February 2, 2015 11:49 AM
  • DayOfWeek is an enum (Sunday = 0, Monday = 1, ... Saturday = 6), so you can do this:

    $NextSunday = 
    (Get-Date).adddays((7..1)[[int](Get-Date).DayOfWeek])
    
    $NextMonday = 
    (Get-Date).adddays((7..1)[[int](Get-Date).DayOfWeek -1])
    
    $NextTuesday = 
    (Get-Date).adddays((7..1)[[int](Get-Date).DayOfWeek -2 ])
    
    ...
    
    $NextSaturday = 
    (Get-Date).adddays((7..1)[[int](Get-Date).DayOfWeek -6])


    
    

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "





    Monday, February 2, 2015 12:07 PM
    Moderator
  • In simple constructs the releative difference in speed is unimportant.  In software engineering we code for clarity and reliability first.  Performance is addressed in the overall design process and in post code metrics.  The old idea that using a 5 character label is more efficient that a 6 character label went out with the the idea that a "GOTO" was faster than a subroutine call.

    Old thinking has no place in  modern systems. 

    When we are dealing with this kind of issue the most efficient and regular solution can be found by understanding that weekdays are a MOD 7 mathematical pattern.  Do the math.


    ¯\_(ツ)_/¯

    Monday, February 2, 2015 12:37 PM
  • Here is an example of a declaritive approach to the "next saturday" problem.

    $anydate.AddDays($(if(($x=6-[int]$d.DayOfWeek) -eq 0){7}else{$x}))

    This use the remainder or residue technique as it is "faster" by about 5 CPU cycles then doing a "MOD" operation.  This is so Sam doesn't compein about wasted processor cycles;)


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, February 2, 2015 1:32 PM
    Monday, February 2, 2015 1:32 PM