Windows/PowerShell&Command
PowerShell(파워쉘) 날짜 포맷 변경 및 날짜 계산하기
Hoonjo
2023. 6. 29. 19:30
PowerShell(파워쉘) 날짜 포맷 변경 및 날짜 계산하기
PowerShell Change the date format and calculate the date
PowerShell(파워쉘)에서 날짜 포맷 변경 방법과 날짜 계산하는 방법을 알아보겠습니다.
Get-Date 명령어를 사용하면 현재시각을 가져올 수 있습니다.
1. 날짜 포맷 변경하기
-Uformat 옵션으로 날짜 출력 포맷을 변경하여 원하는 형태로 출력할 수 있습니다.
명령어:
Get-Date -Uformat "test_%Y%m%d.log"
2. 날짜 계산하기
일(Day) 변경은 AddDays(숫자)를 통해 계산할 수 있습니다.
명령어:
Get-Date -Date (Get-Date).AddDays(-1)
-1을 입력해서 전 날(yesterday)이 출력되었습니다.
같은 방법으로 년, 월도 변경 가능합니다.
월(Month) 변경 - AddMonths
명령어:
Get-Date -Date (Get-Date).AddMonths(-1)
년(Year) 변경 - AddYears
명령어:
Get-Date -Date (Get-Date).AddYears(-1)
3. 날짜 포맷 변경 및 계산
날짜를 계산해서 포맷이 변경된 형태로 출력해 보겠습니다.
명령어:
Get-Date -Date (Get-Date).AddDays(-1) -Uformat "test_%Y%m%d.log"