Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

All Those Years Ago

Aug
1,914
68
Demonstration of how to get date properties using PowerShell, based on the date x years ago, or x years from now.

Code:
@setlocal
@echo off
function AddYears=`%@pshell[$d = Get-Date; $d.AddYears(%1)]`
function AddYears.DayOfWeek=`%@pshell[$d = Get-Date; $d.AddYears(%1).DayOfWeek]`
function AddYears.ToShortDate=`%@pshell[$d = Get-Date; $d.AddYears(%1).ToShortDateString()]`
function AddYears.ToLongDate=`%@pshell[$d = Get-Date; $d.AddYears(%1).ToLongDateString()]`
function AddYears.Day=`%@pshell[$d = Get-Date; $d.AddYears(%1).Day]`
function AddYears.Month=`%@pshell[$d = Get-Date; $d.AddYears(%1).Month]`
function AddYears.DayOfYear=`%@pshell[$d = Get-Date; $d.AddYears(%1).DayOfYear]`

set theNumber=-72
ver
echo.
echo Todays date is %@AddYears[0]
echo.
echo %((%theNumber*-1)) years ago, the date was %@AddYears[%theNumber]
echo.
echo The short date was %@AddYears.ToShortDate[%theNumber]
echo.
echo The long date was %@AddYears.ToLongDate[%theNumber]
echo.
echo The day of the week was %@AddYears.DayOfWeek[%theNumber]
echo.
echo The day was %@AddYears.Day[%theNumber]
echo.
echo The month was %@AddYears.Month[%theNumber]
echo.
echo The Day of the year was %@AddYears.DayOfYear[%theNumber]
endlocal
quit

TCC  22.00.38 x64   Windows 7 [Version 6.1.7601]

Todays date is 
Wednesday, February 14, 2018 6:06:57 AM

72 years ago, the date was 
Thursday, February 14, 1946 6:06:57 AM

The short date was 2/14/1946

The long date was Thursday, February 14, 1946

The day of the week was Thursday

The day was 14

The month was 2

The Day of the year was 45

Additional References:
Get-Date
Date and Time formats


Joe
 
Back
Top