Solved

Powershell Date help

Posted on 2013-11-05
3
400 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
Comment Utility
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
Comment Utility
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
Comment Utility
Awesome :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

728 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

14 Experts available now in Live!

Get 1:1 Help Now