{"id":628,"date":"2008-09-04T08:45:32","date_gmt":"2008-09-04T16:45:32","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/09\/04\/get-otherdate\/"},"modified":"2008-09-04T08:45:32","modified_gmt":"2008-09-04T16:45:32","slug":"get-otherdate","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/09\/04\/get-otherdate\/","title":{"rendered":"Get-OtherDate"},"content":{"rendered":"<p>PowerShell&#8217;s [DATETIME] object has some handy methods for calculating a date such as <strong>AddDays<\/strong>().<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; get-date\n\nWednesday, September 03, 2008 4:11:18 PM\n\nPS C:\\&gt; (get-date).AddDays(23)\n\nFriday, September 26, 2008 4:11:34 PM\n\nPS C:\\&gt;<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can even subtract by adding a negative number:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; (get-date).AddHours(-67)\n\nSunday, August 31, 2008 9:14:40 PM<\/font>\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The [DATETIME] object also allows you to add a TimeSpan like this:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; [datetime]$d=\"2\/22\/2008 5:00AM\"\nPS C:\\&gt; #Add 2 days<br>PS C:\\&gt; $d.Add(\"2:0:0\")\n\nFriday, February 22, 2008 7:00:00 AM\nPS C:\\&gt; #Add 34 minutes\nPS C:\\&gt; $d.Add(\"0:34:0\")\n\nFriday, February 22, 2008 5:34:00 AM\n\nPS C:\\&gt; #Add 15 seconds\nPS C:\\&gt; $d.Add(\"0:0:15\")\n\nFriday, February 22, 2008 5:00:15 AM\n\nPS C:\\&gt; #Add 2 days, 3 hours and 15 min<br>PS C:\\&gt; $d.Add(\"2:3:15\")\n\nFriday, February 22, 2008 7:03:15 AM<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>This is all nice. But what if you wanted to calculate a more complex date time, perhaps one in years? There is an <strong>AddYears()<\/strong> method. Although none of this will help if you want to calculate a date that is 1 year, 2 months&nbsp; and 1 day from now. For those situations, or if you want a simpler approach instead of calling methods, I wrote the <strong><font color=\"#0000ff\">Get-OtherDate<\/font><\/strong> function.<\/p>\n<div style=\"border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4\">\n<div style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\">\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">   1:<\/span> Function Get-OtherDate {<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">   2:<\/span>     Param([datetime]$date=(Get-Date),<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">   3:<\/span>           [int32]$years=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">   4:<\/span>           [int32]$months=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">   5:<\/span>           [int32]$days=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">   6:<\/span>           [int32]$hours=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">   7:<\/span>           [int32]$minutes=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">   8:<\/span>           [int32]$seconds=0,<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">   9:<\/span>           [int32]$milliseconds=0<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  10:<\/span>           )<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  11:<\/span>           <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  12:<\/span>     [datetime]$dt=$date   <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  13:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($milliseconds <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddMilliseconds($milliseconds)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  14:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($seconds <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddSeconds($seconds)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  15:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($minutes <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddMinutes($minutes)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  16:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($hours <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddHours($hours)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  17:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($days <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddDays($days)}     <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  18:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($months <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddMonths($months)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  19:<\/span>     <span style=\"color: #0000ff\">if<\/span> ($years <span style=\"color: #cc6633\">-ne<\/span> 0) {$dt=$dt.AddYears($years)}<\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  20:<\/span>  <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  21:<\/span>     write $dt          <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none\"><span style=\"color: #606060\">  22:<\/span>   <\/pre>\n<pre style=\"padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none\"><span style=\"color: #606060\">  23:<\/span> }<\/pre>\n<\/div>\n<\/div>\n<p>The function takes as parameters a starting date ($date) and then one or more parameters for time intervals such as -days, -hours or -minutes.&nbsp; You don&#8217;t have to specify a starting date, it will default to the current date and time.&nbsp; All the function does is call the appropriate [datetime] method and builds a new date time object moving&nbsp; from the smallest time interval to the largest. Here are some examples of the function in use with the default current date:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; get-date\n\nThursday, September 04, 2008 12:15:43 PM\n\nPS C:\\&gt; Get-OtherDate -months 1 -days 11 -hours 7\n\nWednesday, October 15, 2008 7:15:46 PM\n\nPS C:\\&gt; Get-OtherDate -days -45\n\nMonday, July 21, 2008 12:16:08 PM<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here are some samples using a different date:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; get-OtherDate 7\/4\/2008 -days 99\n\nSaturday, October 11, 2008 12:00:00 AM\n\nPS C:\\&gt; Get-OtherDate 12:34 -hours 4\n\nThursday, September 04, 2008 4:34:00 PM\n\nPS C:\\&gt; Get-OtherDate 22:34 -hours -4 -minutes -14\n\nThursday, September 04, 2008 6:20:00 PM<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There&#8217;s nothing really special here.&nbsp; All I&#8217;ve done is create a shortcut function. An advantage is that I can create an alias (like od) and take advantage of short parameter names. Here&#8217;s how this might work in a production environment:<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>\n<pre><font color=\"#0000ff\">PS C:\\&gt; [ADSI]$admin=\"WinNT:\/\/$env:computername\/Administrator,user\"\nPS C:\\&gt; $lastset = od -s (-$admin.passwordage.value)\nPS C:\\&gt; $expires = od $lastset -day 60\nPS C:\\&gt; Write-host \"Password last set $lastSet and expires $expires\"\nPassword last set 09\/02\/2008 22:29:51 and expires 11\/01\/2008 22:29:51\nPS C:\\&gt;<\/font><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"wlWriterSmartContent\" id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:f094b7c9-0e3e-48b2-a70c-ca67a4893d90\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>Download the script file <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/09\/get-otherdate.txt\" target=\"_blank\">here<\/a><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>PowerShell&#8217;s [DATETIME] object has some handy methods for calculating a date such as AddDays(). Let me show you how as well as my own function for those situations where PowerShell&#8217;s methods just aren&#8217;t enough.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[65,281,282,28,57],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-automation","tag-datetime","tag-functions","tag-powershell","tag-scripting"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}