EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Date
Secondary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Date

By Priya PedamkarPriya Pedamkar

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.

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Input:

Get-Date

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,241 ratings)

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
Popular Course in this category
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more