Solved

Powershell Date help

Posted on 2013-11-05
3
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

631 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