{"id":8281,"date":"2015-01-20T00:01:00","date_gmt":"2015-01-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/20\/use-powershell-to-work-with-individual-dates\/"},"modified":"2019-02-18T10:35:53","modified_gmt":"2019-02-18T17:35:53","slug":"use-powershell-to-work-with-individual-dates","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-work-with-individual-dates\/","title":{"rendered":"Use PowerShell to Work with Individual Dates"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with dates.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. I don&rsquo;t know about you, but one thing about my life as the Scripting Guy is that it keeps me busy. &#8220;Keeping busy&#8221; is a slang expression that means, &#8220;Dude, you are swamped.&#8221;\nThe Scripting Wife and I just spent most of last week planning events for the remainder of 2015. Yep, my year is pretty well planned out. Not because I love being super organized. In fact, my default behavior is rather the opposite&mdash;I prefer a more natural, organic schedule. But the only way I can get to everything is if I have a plan.\nThis means that I spend a lot of time with dates and calendars. Actually, the Scripting Wife is the one who keeps up with that stuff because she really is organized. In fact, I have seen more than one tweet or Facebook post that said something to the effect that if you want me to do something, you better organize it with the Scripting Wife. She is great. Anyway&hellip;\nOne of the really cool things about Windows PowerShell is that it makes it easy for me to see what day the week a specific date will occur. It also makes it pretty easy for me to see how many days remain until a specific date occurs. There are several ways I can create dates.\nYesterday I talked a little bit about System.DateTime objects. If I create a specific date, instead of just returning today&#8217;s date, I am creating a <b>DateTime<\/b> object that represents a date in the future or the past. One way to create a specific date is to use the <b>Get-Date<\/b> cmdlet and then specify values for the <b>&ndash;Day<\/b>, <b>&ndash;Month<\/b>, and <b>&ndash;Year<\/b> parameters. For example, you may know that William Shakespeare died on April 23, 1616. However, most people do not know that that was a Saturday. Here is how I found out that bit of trivia:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Get-Date -Month 4 -Day 23 -Year 1616<\/p>\n<p style=\"margin-left:30px\">Saturday, April 23, 1616 2:20:19 PM\nWhen I use <b>Get-Date<\/b> in this way, it returns a <b>DateTime<\/b> object (of course), but it is interesting that because I did not specify a time portion of the <b>DateTime<\/b> object, it uses my local system time. So, I ran the command above at 2:20:19 PM.\nThe <b>Get-Date<\/b> cmdlet will also accept a <b>DateTime<\/b> object as input. I found this out by looking at the syntax of the command. When I use <b>Get-Help<\/b> to look at the syntax, I see that the <b>&ndash;Date<\/b> parameter accepts &lt;DateTime&gt; for input. This tells me that the input value is expected to be an instance of the System.DateTime object. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; (Get-Help get-date).syntax<\/p>\n<p style=\"margin-left:30px\">Get-Date [[-Date] &lt;DateTime&gt;] [-Day &lt;Int32&gt;] [-DisplayHint &lt;DisplayHintType&gt;] [-Format &lt;String&gt;] [-Hour<\/p>\n<p style=\"margin-left:30px\">&lt;Int32&gt;] [-Millisecond &lt;Int32&gt;] [-Minute &lt;Int32&gt;] [-Month &lt;Int32&gt;] [-Second &lt;Int32&gt;] [-Year &lt;Int32&gt;]<\/p>\n<p style=\"margin-left:30px\">[&lt;CommonParameters&gt;]<\/p>\n<p style=\"margin-left:30px\">Get-Date [[-Date] &lt;DateTime&gt;] [-Day &lt;Int32&gt;] [-DisplayHint &lt;DisplayHintType&gt;] [-Hour &lt;Int32&gt;]<\/p>\n<p style=\"margin-left:30px\">[-Millisecond &lt;Int32&gt;] [-Minute &lt;Int32&gt;] [-Month &lt;Int32&gt;] [-Second &lt;Int32&gt;] [-UFormat &lt;String&gt;] [-Year<\/p>\n<p style=\"margin-left:30px\">&lt;Int32&gt;] [&lt;CommonParameters&gt;]\nBecause Windows PowerShell expects a <b>DateTime<\/b> object for input to the <b>&ndash;Date<\/b> parameter, it will be happy with anything that I supply to the parameter that it can convert into an instance of a <b>DateTime<\/b> object. This is because Windows PowerShell does automatic type conversion. So anything that is reasonably formatted will be converted.\nTo manually convert a string that represents a date into a <b>DateTime<\/b> object, I can use the type accelerator, [DateTime], and it will convert (cast) the string to the object. Here is an example:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; [datetime]&#8221;June 15, 1215&#8243;<\/p>\n<p style=\"margin-left:30px\">Monday, June 15, 1215 12:00:00 AM\nTo prove that I actually converted the string to an instance of the System.DateTime object, I can pipe the results into the <b>Get-Member<\/b> cmdlet, for example:<\/p>\n<p style=\"margin-left:30px\">[datetime]&#8221;June 15, 1215&#8243; | Get-Member&nbsp;\nI can also use the <b>GetType<\/b> method directly from the expression. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; ([datetime]&#8221;June 15, 1215&#8243;).GetType()<\/p>\n<p style=\"margin-left:30px\">IsPublic IsSerial Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BaseType&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8211; &#8212;&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"margin-left:30px\">True&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp; DateTime &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.ValueType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\nSo, because I can convert a string to a <b>DateTime<\/b> object, and because the <b>Get-Date<\/b> cmdlet accepts a <b>DateTime<\/b> object as input, I have seen people do something like the following:<\/p>\n<p style=\"margin-left:30px\">Get-date -date ([datetime]&#8221;June 15, 1215&#8243;)\nThis works, but dude, it is a lot of extra work. In fact, it does not do any more than the previous expression. So how does <b>Get-Date<\/b> &ldquo;June 15, 1215&rdquo; actually work?\nWell, the default parameter of <b>Get-Date<\/b> is actually <b>&ndash;Date<\/b>. And as I said earlier, it expects a <b>DateTime<\/b> object, and therefore it will try to convert whatever I give it into a <b>DateTime<\/b> object. I can see that <b>&ndash;Date<\/b> is the default parameter by using the <b>Get-Help<\/b> cmdlet. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; Get-Help get-date -Parameter date<\/p>\n<p style=\"margin-left:30px\">-Date &lt;DateTime&gt;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Specifies a date and time. By default, Get-Date gets the current system date and time.<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Type the date in a format that is standard for the system locale, such as dd-MM-yyyy (German<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; [Germany]) or MM\/dd\/yyyy (English [United States]).<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Required?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; false<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Position?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Default value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Current date<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Accept pipeline input?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True (ByValue, ByPropertyName)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Accept wildcard characters?&nbsp; false\nWindows PowerShell is pretty good when it comes to converting my input date value. Here are some examples that it translates into a <b>DateTime<\/b> object:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date 6-15-1215<\/p>\n<p style=\"margin-left:30px\">Monday, June 15, 1215 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date 6\/15\/1215<\/p>\n<p style=\"margin-left:30px\">Monday, June 15, 1215 12:00:00 AM<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date 6.15.1215<\/p>\n<p style=\"margin-left:30px\">Monday, June 15, 1215 12:00:00 AM\nIt will even accept abbreviations. Here is an example of using an abbreviation:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date &#8220;Jun. 15, 1215&#8221;<\/p>\n<p style=\"margin-left:30px\">Monday, June 15, 1215 12:00:00 AM\nBut if I do not use quotation marks, I get an error message. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date Jun. 15, 1215<\/p>\n<p style=\"margin-left:30px\">Get-Date : Cannot bind parameter &#8216;Date&#8217;. Cannot convert value &#8220;Jun.&#8221; to type &#8220;System.DateTime&#8221;. Error:<\/p>\n<p style=\"margin-left:30px\">&#8220;String was not recognized as a valid DateTime.&#8221;<\/p>\n<p style=\"margin-left:30px\">At line:1 char:10<\/p>\n<p style=\"margin-left:30px\">+ get-date Jun. 15, 1215<\/p>\n<p style=\"margin-left:30px\">+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidArgument: (:) [Get-Date], ParameterBindingException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand\nIn addition, if I use the wrong symbol for a separator it generates an error message:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date 6151215<\/p>\n<p style=\"margin-left:30px\">Get-Date : Cannot bind parameter &#8216;Date&#8217;. Cannot convert value &#8220;6151215&#8221; to type &#8220;System.DateTime&#8221;.<\/p>\n<p style=\"margin-left:30px\">Error: &#8220;String was not recognized as a valid DateTime.&#8221;<\/p>\n<p style=\"margin-left:30px\">At line:1 char:10<\/p>\n<p style=\"margin-left:30px\">+ get-date 6151215<\/p>\n<p style=\"margin-left:30px\">+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~~~~~~~~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidArgument: (:) [Get-Date], ParameterBindingException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand\nHere is some fun trivia. If I do not remember how many days are in a month (such as February), I can wing it. So February 31, 2015 actually becomes March 3 (that is three days after the end of February, which is February 28. Here is what I am talking about:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date -Day 31 -Month 2 -Year 2015<\/p>\n<p style=\"margin-left:30px\">Tuesday, March 3, 2015 3:24:06 PM\nI thought, &#8220;Wow, that is pretty cool. I wonder if I can, say, add 30 days to February 15, and come up with something like March the 17?&#8221; So I tried it:<\/p>\n<p style=\"margin-left:30px\">PS C:&gt; get-date -Day 45 -Month 2 -Year 2015<\/p>\n<p style=\"margin-left:30px\">Get-Date : Cannot validate argument on parameter &#8216;Day&#8217;. The 45 argument is greater than the maximum<\/p>\n<p style=\"margin-left:30px\">allowed range of 31. Supply an argument that is less than or equal to 31 and then try the command again.<\/p>\n<p style=\"margin-left:30px\">At line:1 char:15<\/p>\n<p style=\"margin-left:30px\">+ get-date -Day 45 -Month 2 -Year 2015<\/p>\n<p style=\"margin-left:30px\">+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ~~<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + CategoryInfo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : InvalidData: (:) [Get-Date], ParameterBindingValidationException<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetDateComman<span style=\"font-size:12px\">d<\/span>\nNope. The error message tells me that the maximum value of <strong>-Day&nbsp;<\/strong>is 31, which makes sense, I guess.\nThat is all there is to working with dates in Windows PowerShell. Date Time Week will continue tomorrow when I will talk about adding and subtracting days.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with dates. Microsoft Scripting Guy, Ed Wilson, is here. I don&rsquo;t know about you, but one thing about my life as the Scripting Guy is that it keeps me busy. &#8220;Keeping busy&#8221; is a slang expression that means, &#8220;Dude, you are swamped.&#8221; [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,3,4,45],"class_list":["post-8281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to work with dates. Microsoft Scripting Guy, Ed Wilson, is here. I don&rsquo;t know about you, but one thing about my life as the Scripting Guy is that it keeps me busy. &#8220;Keeping busy&#8221; is a slang expression that means, &#8220;Dude, you are swamped.&#8221; [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=8281"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/8281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=8281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=8281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=8281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}