Solved

Powershell Date help

Posted on 2013-11-05
3
405 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to strip content from csv 8 17
Powershell Exchange mailboxsizes 3 11
PowerShell:  Column widths won't expand 3 23
mount point report 5 13
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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).
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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