Solved

PowerShell Code - Is a date in this Array/HashTable

Posted on 2015-02-24
6
127 Views
Last Modified: 2015-02-24
Okay, I'm still learning, and I am trying to write a function (which will basically be recursive) to return a 'good' date. I want to build either an array or hash table (not sure which) of holidays. I do not have any reason to retrieve a specific holiday, I only want to know if a specific date is in the table.
Basically, I pass a date to the function. Using Switch() if it is a Saturday, Sunday or Monday, I subtract either 1, 2 or 3 days respectively. Any other date, I subtract 1 day. Then I want to check if the (adjusted) date is in the array/hash table. If it is, I want to call the function again, passing date - 1. The return value will be a good date.

Thus, if I am working with a Tuesday and the previous day is a holiday, the call to the function would calculate the Monday, determine it is a holiday, and call the function passing Sunday's date (calculated Monday - 1), which would in turn return Friday's date.

Here is what I have so far, but I think, from what I'm reading, what I really need is a hash table. Just having a bit of trouble putting it together. Note that the value passed (and needed to be returned) for $procDate is a string in the format of yyyy-mm-dd. As I indicated, I do not need to know what the date is, associate it with any specific holiday, etc. I just need to know if the date I am working with is in the array/table.
$holidays = @("2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19")

Function GetWorkDay($procDate)
{
	$dow = (Get-Date($procDate)).DayOfWeek
	$procDate = Switch ($dow)
	{
		"Saturday" { (Get-Date($processDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		"Sunday" { (Get-Date($processDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		"Monday" { (Get-Date($processDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		default { (Get-Date($processDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	}
	If ($procDate is a holiday) # pseudo-code
	{
		$procDate = GetWorkDay((Get-Date($procDate)).AddDays(-1))
	}
	Return $procDate
}

Open in new window

0
Comment
Question by:dbbishop
  • 3
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 40629334
Hi, first off, an array would be more suitable for single values.  Second, in your Switch you had $processDate and not $procDate.

Then, to determine whether the date exists, we use
$holidays -contains $procDate

Regards,

Rob.

$holidays = "2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19"

Function GetWorkDay($procDate)
{
	$dow = (Get-Date($procDate)).DayOfWeek
	$procDate = Switch ($dow)
	{
		"Saturday" { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		"Sunday" { (Get-Date($procDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		"Monday" { (Get-Date($procDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		default { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	}
    If ($holidays -contains $procDate)
	{
        $procDate = GetWorkDay((Get-Date($procDate)).AddDays(-1))
	}
	Return $procDate
}

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 40629386
You can avoid some iteration for the holiday search if you use the -contains operator.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40629440
In the following example, the $d variable, contains a datetime value.
Example:
$holidays -contains (get-date -date $d.adddays(-1) -uformat "%Y-%m-%d")

Open in new window


You can separate some of these functions, updating your $d variable, as you step back in time.
$d = $d.adddays(-1)
$holidays -contains (get-date -date $d -uformat "%Y-%m-%d")

Open in new window

0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 65

Expert Comment

by:RobSampson
ID: 40629461
I haven't tested this too much, but you can also do away with the recursive call by using a loop that rechecks the date.

Rob.

$holidays = "2013-11-28","2013-12-25","2014-01-01","2014-01-20","2014-02-17","2014-04-18","2014-05-26","2014-07-04","2014-09-01","2014-02-17","2014-11-27","2014-12-25","2014-01-01","2014-01-19"

Function GetWorkDay($procDate)
{
    $recheck = $true
	While ($recheck -eq $true)
    {
        $recheck = $false
        $dow = (Get-Date($procDate)).DayOfWeek
	    $procDate = Switch ($dow)
	    {
		    "Saturday" { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
		    "Sunday" { (Get-Date($procDate)).AddDays(-2).ToString("yyyy-MM-dd") }
		    "Monday" { (Get-Date($procDate)).AddDays(-3).ToString("yyyy-MM-dd") }
		    default { (Get-Date($procDate)).AddDays(-1).ToString("yyyy-MM-dd") }
	    }
        If ($holidays -contains $procDate)
	    {
            $procDate = (Get-Date($procDate)).AddDays(-1)
            $recheck = $true
	    }
    }
	Return $procDate
}

Open in new window

0
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 40629830
Had I not been working 50-60 hours/week for the last couple of weeks on a project that has everyone breathing down my neck, I might of figured that one out, because I think I did something similar awhile back. But, I gotta love EE. Thanks!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40629835
No problem, we're here to help (and learn)!  ;-)

Rob.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question