Solved

Powershell Date help

Posted on 2013-11-05
3
404 Views
Last Modified: 2013-11-05
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
0
Comment
Question by:mikeyd234
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39625438
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
 
LVL 3

Author Comment

by:mikeyd234
ID: 39625523
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
 
LVL 3

Author Closing Comment

by:mikeyd234
ID: 39625526
Awesome :)
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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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