{"id":78895,"date":"2016-06-21T00:01:38","date_gmt":"2016-06-21T07:01:38","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=78895"},"modified":"2019-02-18T09:10:37","modified_gmt":"2019-02-18T16:10:37","slug":"build-a-hexadecimal-clock-in-powershell-part-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/build-a-hexadecimal-clock-in-powershell-part-2\/","title":{"rendered":"Build a hexadecimal clock in PowerShell \u2013 Part 2"},"content":{"rendered":"<p><strong>Summary<\/strong>: Manipulate string data from Get-Date in PowerShell.<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today to have a little more fun with our silly project to build a hexadecimal clock in the PowerShell console.<\/p>\n<p>Well, after all, who said scripting wasn\u2019t allowed to be fun? That\u2019s usually how I learn, by playing about!<\/p>\n<p>Yesterday, we defined a cool array of <strong>Here-String<\/strong>s that contain all the characters that we\u2019d like to display in our clock. Today, we\u2019re going to access the properties of the current time and see how we could put that to use.<\/p>\n<p>The <u>very<\/u> first thing that we\u2019ll need to do is pull up the date and time and store that away in an object.<\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date<\/code><\/p>\n<p>Good. We\u2019ve got the current date and time. If you run <strong>Get-Member<\/strong> against this, you\u2019ll see some <u>very<\/u> useful properties, namely, hour, minute, and second.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062116.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78896\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1-HSG-062116.png\" alt=\"Screenshot of results of Get-Member which returns hour, minute, and second among other properties.\" width=\"413\" height=\"285\" \/><\/a><\/p>\n<p>We can access any one of these objects by just connecting the dots. No, literally!<\/p>\n<p style=\"padding-left: 60px\"><code>$Now.Hour\n$Now.Minute\n$Now.Second<\/code><\/p>\n<p>This is perfect! We can easily access the data we need. Now, all we need to do is get each one as a hexadecimal number.<\/p>\n<p>To do this, we just need to use this <strong>[Convert]<\/strong> accelerator. Converting a single number to hexadecimal in PowerShell is easy. Here, we convert 42 to Hex:<\/p>\n<p style=\"padding-left: 60px\"><code>[Convert]::tostring(42,16)<\/code><\/p>\n<p>If you <span style=\"text-decoration: underline\">really<\/span>\u00a0want to be nerdy, you could try Octal by changing the 16 to an 8 for\u2026\u201cBase 8\u201d.<\/p>\n<p style=\"padding-left: 60px\"><code>[Convert]::tostring(42,8)<\/code><\/p>\n<p>So, for each number, we\u2019ll need to convert the information to a hex digit like so:<\/p>\n<p style=\"padding-left: 60px\"><code>$HexHour=[Convert]::tostring($Now.Hour,16)\n$HexMinute=[Convert]::tostring($Now.Minute,16)\n$HexSecond=[Convert]::tostring($Now.Second,16)<\/code><\/p>\n<p>But, we have one extra problem. When I\u2019m done, I want to have two digits for every column. I want to have this evenly spaced and have a consistent number of characters.<\/p>\n<p>A built-in method that we can use for strings is <strong>padleft()<\/strong>, which allows us to specify how many characters the output <u>should<\/u> have at minimum and what and where to \u201cpad the blanks with\u201d.<\/p>\n<p>The <strong>padleft<\/strong> method requires two properties: the number of characters and just what you\u2019re going to pad it with.<\/p>\n<p>We will make sure that each position has two (2) characters and will always have <u>at<\/u> <u>least<\/u> a single \u20180\u2019 preceding it. It (padleft) will look like this:<\/p>\n<p style=\"padding-left: 60px\"><code>Padleft(2,\u20190\u2019)<\/code><\/p>\n<p>So, when we pull up the time and convert hours, minutes, and seconds to hex, it will look like this:<\/p>\n<p style=\"padding-left: 60px\"><code>$HexHour=[Convert]::tostring($Now.Hour,16).padleft(2,\u20190\u2019)\n$HexMinute=[Convert]::tostring($Now.Minute,16).padleft(2,\u20190\u2019)\n$HexSecond=[Convert]::tostring($Now.Second,16).padleft(2,\u20190\u2019)<\/code><\/p>\n<p>For the final time piece, we\u2019ll wrap it all together as a single string and make this a basic function:<\/p>\n<p style=\"padding-left: 60px\"><code>Function Get-HexTime()<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>{<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>$Now=Get-Date\n$hour=[Convert]::tostring(($now.Hour),16).padleft(2,'0')\n$Minute=[Convert]::tostring(($now.Minute),16).padleft(2,'0')\n$Second=[Convert]::tostring(($now.Second),16).padleft(2,'0')<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>Return (\"$Hour-$Minute-$Second\")<\/code><\/p>\n<p style=\"padding-left: 60px\"><code>}<\/code><\/p>\n<p>Getting the time in hex is <u>very<\/u> easy now. We just execute the <strong>Get-HexTime<\/strong> function.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062116.png\"><img decoding=\"async\" class=\"alignnone size-full wp-image-78905\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2-HSG-062116.png\" alt=\"Screenshot of the Get-HexTime function.\" width=\"219\" height=\"58\" \/><\/a><\/p>\n<p>Swing on over to the Scripting Guys tomorrow when we start to play with how to parse this data and access the array!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto: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.<\/p>\n<p>Until then always remember that with Great PowerShell comes Great Responsibility.<\/p>\n<p><strong>Sean Kearney\n<\/strong>Honorary Scripting Guy\nCloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Manipulate string data from Get-Date in PowerShell. Honorary Scripting Guy, Sean Kearney, is here today to have a little more fun with our silly project to build a hexadecimal clock in the PowerShell console. Well, after all, who said scripting wasn\u2019t allowed to be fun? That\u2019s usually how I learn, by playing about! Yesterday, [&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":[568,685,641],"tags":[56,154,45],"class_list":["post-78895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","category-scripting-techniques","category-windows-powershell","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Manipulate string data from Get-Date in PowerShell. Honorary Scripting Guy, Sean Kearney, is here today to have a little more fun with our silly project to build a hexadecimal clock in the PowerShell console. Well, after all, who said scripting wasn\u2019t allowed to be fun? That\u2019s usually how I learn, by playing about! Yesterday, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78895","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=78895"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/78895\/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=78895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=78895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=78895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}