Example of using Get-date to obtain datetime and format output in PowerShell _powershell

Source: Internet
Author: User
Tags datetime

There is a Get-date cmdlet in PowerShell that you can use to return the current date and time directly. Use the-format parameter to return the current year, month, day, time, minutes, seconds, and so on.

Direct use of Get-date

Call Get-date directly in PowerShell, and you can return the current date and time, including year, month, day, time, minutes, and seconds. Examples are as follows:

Copy Code code as follows:

PS c:\users\zhanghong> Get-date
September 9, 2013 22:26:56

Use Get-date in Write-host

Note: Using Get-date in Write-host, we can only get a string with the content "get-date", the effect is as follows:

Copy Code code as follows:

PS c:\users\zhanghong> write-host "get-date, helllo!"
Get-date, helllo!

If you want to output the current time in Write-host, you can use the get-date as a variable, using "$ ()" to get it. Use the following methods:
Copy Code code as follows:

PS c:\users\zhanghong> write-host "$ (get-date), hello!"
09/09/2013 22:29:41, hello!

Use the format parameter to get year, month, day, hour, minute, second

Let's see how to use the-format parameter to get the year, month, day, time, minute, and second in Get-date. Remember one of the following relationships first:

Copy Code code as follows:

YYYY years
M Month
D-Day
H-hour (12-hour system)
H-hour (24-hour system)
M minutes
s seconds

Then take a look at the examples to understand:

Copy Code code as follows:

PS c:\users\zhanghong> get-date-format ' yyyy '
2013

About PowerShell use Get-date this cmdlet to get the date and time, small series on the introduction so much, hope to help everyone.

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.