EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Date
 

PowerShell Date

Priya Pedamkar
Article byPriya Pedamkar

Updated March 23, 2023

PowerShell-Date

 

 

Introduction to PowerShell Date

This article will cover the Get-Date cmdlet in PowerShell. It returns the current date. The default format of the date returned will be the same as the format of the date format maintained in the local system. There are multiple formats and different parameters that are associated with the Get-Date cmdlet which will be covered in the upcoming sections in the article. To know the current format of the date in the local system, (Get-Culture). DateTimeFormat can be used.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax of PowerShell Date

The syntax of the Get-Date is as follows:

Get-Date
[[-Date] <DateTime>] [-Year <Int32>] [-Month <Int32>] [-Day <Int32>] [-Hour <Int32>] [-Minute <Int32>] [-Second <Int32>] [-Millisecond <Int32>] [-DisplayHint <DisplayHintType>] [-Format <String>] [<CommonParameters>]

Eg: Get-Date will return the below output

Input:

Get-Date

Output:

Get date windows

Let us understand in detail the various parameters that are associated with the Get-Date cmdlet

Parameters

1. Date: This is an optional parameter. Its data type is DateTime. It doesn’t accept wild characters and the default value is none. LastWriteTime is the alias name. It specifies a date and time.

Eg: Get-Date -Date “1/1/1920 12:30:22”

Output:

date system

2. Day: This is also an optional parameter. It returns the day of the month whose date is specified. The default value is none and wild card characters are not accepted. It accepts a value from 1 to 31. In case if the number mentioned is greater than the number of days in the month, PowerShell adds it to the month.

E.g.: Get-Date -Month 11 -Day 19

Output:

months

3. Format: This specifies the date format. Its output is in a string object. Its type is a string. Default is none and wild card characters are not accepted.

4. Hour: This is used to denote the hour. Its type is int32 and the default value is none. The value should be within the range of 0 to 23. Wild card characters are not accepted.

5. Millisecond: This is used to denote the millisecond. Its type is int32 and the default value is none. The value should be within the range of 0 to 999. Wild card characters are not accepted.

6. Minute: This is used to denote the minute. Its type is int32 and the default value is none. The value should be within the range of 0 to 59. Wild card characters are not accepted.

7. Month: This is used to denote the minute. Its type is int32 and the default value is none. The value should be within the range of 1 to 12. Wild card characters are not accepted.

8. Second: This is used to denote the second. Its type is int32 and the default value is none. The value should be within the range of 1 to 12. Wild card characters are not accepted.

9. Year: This is used to denote the year. Its type is int32 and the default value is none. The value should be within the range of 1 to 9999. Wild card characters are not accepted.

Date Options in PowerShell

The following are the various date formats available.

  • d: Denotes ShortDate
  • D: Denotes LongDate
  • f: Denotes long date, the short time
  • F: Denotes long date, long time
  • t: Denotes Short time format
  • T: Denotes Long time formats,
  • M: Month Day format

Examples of Various PowerShell Date

The examples of various date are given below:

Example #1

Input:

Write-Host  "Example of Long Date Format"
Get-Date -Format D
Write-Host  "Example of Long Date and Short time Format"
Get-Date -Format f
Write-Host  "Example of Long Date and Longtime Format"
Get-Date -Format F
Write-Host  "Example of General date-time format(short time)"
Get-Date -Format g
Write-Host  "Example of General date-time format(Long time)"
Get-Date -Format G
Write-Host  "Example of month format"
Get-Date -Format m
Write-Host  "Example of year format"
Get-Date -Format y

Output:

powershell date

Example #2

Different year formats

Input:

Get-Date -Format dd-MMM-yyyy
Get-Date -Format dd-MMMM-yyyy
Get-Date -Format MM-yy
Get-Date -Format MM-yyyy
Get-Date -Format MMMM-yy
Get-Date -Format MMMM-yyyy
Get-Date -Format 'dd-MMMM-yyyy hh:mm:ss tt'
Get-Date -Format 'dd-MMMM-yyyy HH:mm:ss'

Output:

powershell date

Example #3

Get-Date with Format-List

Input:

Get-Date | Format-List

Output:

powershell date

powershell date

Example #4

Find the day of the date in the year

Input:

(Get-Date -the Year 2090 -Month 10 -Day 02).DayOfYear

Output:

date of the year

Example #5

Check for daylight saving

Input:

$date=Get-Date "12/12/2019"
$date.IsDaylightSavingTime()

Output:

false format

Input:

$date=Get-Date "11/1/2019"
$date.IsDaylightSavingTime()

Output:

true format

Examples #6

Input:

$date= Get-Date
#adding 100 days to the current date
$newdate=$date.AddDays(100)
Write-host "New Date is:" $newdate
#converting the current date to universal time
$utime=$date.ToUniversalTime()
Write-Host "Universal time is:" $utime
#add minutes to the current time
$newtime=$date.AddMinutes(35)
Write-Host "Altered time is:" $newtime

Output:

universal time

Example #7

Find the difference between two dates

Input:

$dateA= Get-Date
$dateB= Get-Date "1/1/1947"
$difference= $dateA- $dateB
$difference|Format-List

Output:

difference between two dates

Conclusion

Thus, the article covered in detail in the various date time formats along with examples. The best way to learn about each format available is to try out each format and understand which is suitable for the script need. It is advisable to use the standard format.

Recommended Articles

This is a guide to PowerShell Date. Here we discuss the introduction, Date Options, Examples of Various PowerShell Date along with Syntax, parameters. You can also go through our other suggested articles to learn more–

  1. PowerShell String Functions
  2. What is PowerShell
  3. How To Install PowerShell
  4. Uses Of Powershell
  5. Examples of Powershell Write-Host
  6. Guide to PowerShell Get-Date
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW