PowerShell tips to get the current time and convert to an hour _powershell

Source: Internet
Author: User

Noon three moment has arrived, execution, Liusto, now in the end is not noon, can let PowerShell tell me?

Ok no problem. From 23 o'clock to 2 o'clock in the morning from the night to belong to the midnight, every two hours an hour, in turn, "Kaniko Chen has not Shin hai."

function to get the current hour
Implemented with PowerShell script:

function Get-chinatimealias
{
 param (
 [Validaterange (0,23)]
 [int] $Hour = (get-date). Hour
 )
 $timeAliasArray = ' Kaniko chen has not Shin hai '
 [int] $index =0
 if ($hour-eq) {$index =11}
 else{
 $ Index=[math]::floor (($hour +1)% 23/2)
 } return
 $timeAliasArray [$index] + "when"
}

Get the current hour
Ps> get-date

September 17, 2014 23:17:58

Copy Code code as follows:

Ps> Get-chinatimealias
Midnight

Gets the hour corresponding to the specified number of hours

Copy Code code as follows:

Ps> Get-chinatimealias 12
Noon

Print all hours and corresponding time periods
Input

$timeArray =@ (+0..22) for
($i =0; $i-lt $timeArray. Length; $i = $i +2)
{
 $startHour = $timeArray [$i]. ToString (). PadLeft (2, ' 0 ')
 $endHour = $timeArray [$i +1]. ToString (). PadLeft (2, ' 0 ')
 $timeAlias = Get-chinatimealias $timeArray [$i]
 [pscustomobject]@{
  hour  = $timeAlias;
  Time period = (' {0}:00-{1}:59 '-f $startHour, $endHour)
  }}

Output

Copy Code code as follows:

Hour time period
-- ---
Midnight 23:00-00:59
Shing 01:00-02:59
Yinshi 03:00-04:59
Shing 05:00-06:59
Chen Shi Shenzhou 07:00-08:59
Sishi 09:00-10:59
Noon 11:00-12:59
West 13:00-14:59
Have 15:00-16:59
Youshi 17:00-18:59
Shing 19:00-20:59
Haishi 21:00-22:59

Summarize
The string itself is an array of characters, and there is no need to save Kaniko and so on separately.
Using the model and 22 special treatment to evade the individual conditions of every hour to judge.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.