Powershell Date help

Could someone help me with what i'm sure would be quick powershell function to someone who knows what they are doing. (I'm a bit useless with powershell :( ) :

Parse in 3 parameters to the function, $type, $day, $time (00:00  to 23:59)

If $type is 'daily' then return the date and time of when the next occurrence of $time would be from current date/time.
If $type is 'weekly' then return the date and time of when the next occurrence of $time would be from current date/time but factor in $day - ($day being 1 to 7 int (monday to sunday))
If $type is 'monthly' then return the date and time of when the next occurrence of $time would be from current date/time but factor in $day ($day being 1-28 - days of the month)
If $type is 'yearly' then return the date and time of when the next occurrence of $time would be from current date/time but factor in $day - ($day being 1-364 - day number of the year)

And for a bonus :D

If $type is 'every' then return the date and time of when the next occurrence of $time would be from current date/time - $time being as an example 00:15 - so now + 15 mins time

Even covering just one of the bullets would be helpful, then i can code mashup :P

Thanks in advance,
Mike
LVL 3
mikeyd234Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Rainer JeschorConnect With a Mentor Commented:
Hi,
here we go - please double check the single parts as your calculation rule for monthly and yearly was not 100% clear to me:

function EEDateTimeCalculator([string]$type, $day, $time) {
    $now = Get-Date
    $checkDate = Get-Date -Date $time

    switch ($type.ToLower()) 
    {
        'daily' {
            # check if its today
            if ($checkDate -gt $now) {
                return $checkDate
            } else {
                return $checkDate.AddDays(1)
            }
        }
        'weekly' {
            if ($checkDate.DayOfWeek -eq $day) {
                return $checkDate.AddDays(7)
            } else {
                return $checkDate.AddDays([math]::abs($($day-$checkDate.DayOfWeek)))
            }
        }
        'monthly' {
            if ($day -gt $checkDate.Day) {
                return $checkDate.AddDays($($day-$checkDate.Day))
            } else {
                return $(Get-Date -Year $now.AddMonths(1).Year -Month $now.AddMonths(1).Month -Day $day $time)
            }
        }
        'yearly' {
            if ($day -gt $checkDate.DayOfYear) {
                return $checkDate.AddDays($($day-$checkDate.DayOfYear))
            } else {
                return $(Get-Date -Year $($now.Year +1) -Month 1 -Day 1 $time).AddDays($($day-1)) 
            }
        }
        'every' {
            return  $now.Add($time)
        }

        default { return "Not allowed"} 
    }
}

Write-Host "Call with daily, 2, 12:28 returns $(EEDateTimeCalculator 'Daily' 2 '12:28')"
Write-Host "Call with daily, 2, 22:28 returns $(EEDateTimeCalculator 'Daily' 2 '22:28')"
Write-Host "Call with weekly, 2, 22:28 returns $(EEDateTimeCalculator 'weekly' 2 '22:28')"
Write-Host "Call with weekly, 7, 22:28 returns $(EEDateTimeCalculator 'weekly' 7 '22:28')"
Write-Host "Call with every, 7, 00:15 returns $(EEDateTimeCalculator 'every' 7 '00:15')"
Write-Host "Call with every, 7, 13:00 returns $(EEDateTimeCalculator 'every' 7 '13:00')"
Write-Host "Call with yearly, 32, 13:00 returns $(EEDateTimeCalculator 'yearly' 32 '13:00')"
Write-Host "Call with yearly, 360, 13:00 returns $(EEDateTimeCalculator 'yearly' 360 '13:00')"
Write-Host "Call with monthly, 3, 13:00 returns $(EEDateTimeCalculator 'monthly' 3 '13:00')"
Write-Host "Call with monthly, 28, 13:00 returns $(EEDateTimeCalculator 'monthly' 28 '13:00')"

Open in new window


I added some test outputs based on todays value (November 5th).
Also I did not implement any checks for validity (e.g. monthly -> days must be between 1 and 31(max)).

HTH
Rainer
0
 
mikeyd234Author Commented:
Holy poop, that's awesome. Thanks a lot! Checked them all and you got it spot on.

Thanks again, saved me a lot of time.
0
 
mikeyd234Author Commented:
Awesome :)
0
All Courses

From novice to tech pro — start learning today.