Solved

PowerShell Code - Is a date in this Array/HashTable

Posted on 2015-02-24
6
143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scannerā€¦
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

734 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