Solved

PowerShell Code - Is a date in this Array/HashTable

Posted on 2015-02-24
6
120 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now