Skip to content

Updating Set-Date #4241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 90 additions & 58 deletions reference/3.0/Microsoft.PowerShell.Utility/Set-Date.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,103 @@
---
ms.date: 06/09/2017
ms.date: 4/30/2019
schema: 2.0.0
locale: en-us
keywords: powershell,cmdlet
online version: http://go.microsoft.com/fwlink/?LinkID=113393
external help file: Microsoft.PowerShell.Commands.Utility.dll-Help.xml
title: Set-Date
---

# Set-Date

## SYNOPSIS
Changes the system time on the computer to a time that you specify.

## SYNTAX

### Date (Default)

```
Set-Date [-Date] <DateTime> [-DisplayHint <DisplayHintType>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### Adjust

```
Set-Date [-Adjust] <TimeSpan> [-DisplayHint <DisplayHintType>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
The Set-Date cmdlet changes the system date and time on the computer to a date and time that you specify.
You can specify a new date and/or time by typing a string or by passing a DateTime or TimeSpan object to Set-Date.
To specify a new date or time, use the Date parameter.
To specify a change interval, use the Adjust parameter.
## EXAMPLES

### Example 1
```
C:\PS>Set-Date -Date (Get-Date).AddDays(3)
```
The `Set-Date` cmdlet changes the system date and time on the computer to a date and time that you
specify.
You can specify a new date and/or time by typing a string or by passing a **DateTime** or
**TimeSpan** object to `Set-Date`. To specify a new date or time, use the **Date** parameter.
To specify a change interval, use the **Adjust** parameter.

Description
## EXAMPLES

-----------
### Example 1: Add three days to the system date

This command adds three days to the current system date.
It does not affect the time.
The command uses the Date parameter to specify the date.
It uses the Get-Date cmdlet to get the current date and time and applies the AddDays .NET method for DateTime objects with a value of 3 (days).
### Example 2
```
C:\PS>set-date -adjust -0:10:0 -displayHint time
The command uses the **Date** parameter to specify the date.

The `Get-Date` cmdlet returns the current date as a **DateTime** object. The **DateTime** object's
**AddDays** method adds a specified number of days (3) to the current **DateTime** object.

```powershell
Set-Date -Date (Get-Date).AddDays(3)
```

Description
### Example 2: Set the system clock back 10 minutes

-----------
This example sets the current system time back by 10 minutes.

This command sets the current system time back by 10 minutes.
It uses the Adjust parameter to specify an interval of change and the time change (minus ten minutes) in standard time format for the locale.
The DisplayHint parameter tells Windows PowerShell to display only the time, but it does not affect the DateTime object that Set-Date returns.
### Example 3
```
C:\PS>$t = get-date
PS C:\> set-date -date $t
The **Adjust** parameter allows you to specify an interval of change (minus ten minutes) in the
standard time format for the locale.

The **DisplayHint** parameter tells PowerShell to display only the time, but it does not
affect the **DateTime** object that `Set-Date` returns.

```powershell
Set-Date -Adjust -0:10:0 -DisplayHint Time
```

Description
### Example 3: Set the date and time to a variable value

-----------
These commands change the system date and time on local computer to the date and time saved in the
variable `$T`. The first command gets the date and stores it in `$T`.

These commands change the system date and time on the computer to the date and time saved in the variable $t.
The first command gets the date and stores it in $t.
The second command uses the Date parameter to pass the DateTime object in $t to the Set-Date cmdlet.
### Example 4
```
C:\PS>$90mins = new-timespan -minutes 90
PS C:\> set-date -adjust $90mins
```
The second command uses the **Date** parameter to pass the **DateTime** object in `$T` to the
`Set-Date` cmdlet.

Description
```powershell
$T = Get-Date
Set-Date -Date $T
```

-----------
### Example 4: Add 90 minutes to the system clock

These commands advance the system time on the local computer by 90 minutes.
The first command uses the New-Timespan cmdlet to create a TimeSpan object with a 90-minute interval, and then it saves the TimeSpan object in the $90mins variable.
The second command uses the Adjust parameter of Set-Date to adjust the date by the value of the TimeSpan object in the $90mins variable.

The first command uses the `New-TimeSpan` cmdlet to create a **TimeSpan** object with a 90-minute
interval, and saves it in the `$90mins` variable.

The second command uses the **Adjust** parameter of `Set-Date` to adjust the date by the value of
the **TimeSpan** object in the `$90mins` variable.

```powershell
$90mins = New-TimeSpan -Minutes 90
Set-Date -Adjust $90mins
```

## PARAMETERS

### -Adjust
Adds or subtracts the specified value from the current date and time.
You can type an adjustment in standard date and time format for your locale or use the Adjust parameter to pass a TimeSpan object from New-TimeSpan to Set-Date.

Specifies the value for which this cmdlet adds or subtracts from the current date and time.
can type an adjustment in standard date and time format for your locale or use the **Adjust**
parameter to pass a **TimeSpan** object from `New-TimeSpan` to `Set-Date`.

```yaml
Type: TimeSpan
Expand All @@ -100,12 +112,13 @@ Accept wildcard characters: False
```

### -Date

Changes the date and time to the specified values.
You can type a new date in the short date format and a time in the standard time format for your locale.
Or, you can pass a Date-Time object from Get-Date.
You can type a new date in the short date format and a time in the standard time format for your
locale. Or, you can pass a **DateTime** object from `Get-Date`.

If you specify a date, but not a time, Set-Date changes the time to midnight on the specified date.
If you specify only a time, it does not change the date.
If you specify a date, but not a time, `Set-Date` changes the time to midnight on the specified
date. If you specify only a time, it does not change the date.

```yaml
Type: DateTime
Expand All @@ -120,21 +133,25 @@ Accept wildcard characters: False
```

### -DisplayHint
Determines which elements of the date and time are displayed.

Valid values are:
Specifies which elements of the date and time are displayed.The acceptable values for this parameter
are:

- date: displays only the date
- time: displays only the time
- datetime: displays the date and time
- **Date**.
displays only the date.
- **Time**.
displays only the time.
- **DateTime**.
displays the date and time.

This parameter affects only the display.
It does not affect the DateTime object that Get-Date retrieves.
It does not affect the **DateTime** object that `Get-Date` retrieves.

```yaml
Type: DisplayHintType
Parameter Sets: (All)
Aliases:
Accepted values: Date, Time, DateTime

Required: False
Position: Named
Expand All @@ -144,6 +161,7 @@ Accept wildcard characters: False
```

### -Confirm

Prompts you for confirmation before running the cmdlet.

```yaml
Expand All @@ -159,6 +177,7 @@ Accept wildcard characters: False
```

### -WhatIf

Shows what would happen if the cmdlet runs.
The cmdlet is not run.

Expand All @@ -175,20 +194,33 @@ Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable,
-InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose,
-WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](../Microsoft.PowerShell.Core/About/about_CommonParameters.md).

## INPUTS

### System.DateTime
You can pipe a date to Set-Date.

You can pipe a date to `Set-Date`.

## OUTPUTS

### System.DateTime
Set-Date returns an object that represents the date that it set.

`Set-Date` returns an object that represents the date that it set.

## NOTES
* Use this cmdlet cautiously. Changing the date and time on the computer. The change might prevent the computer from receiving system-wide events and updates that are triggered by a date or time. Use the -WhatIf and -Confirm parameters to avoid errors.

You can use standard .NET methods with the DateTime and TimeSpan objects used with Set-Date, such as AddDays, AddMonths and FromFileTime. For more information, see [DateTime Methods](https://msdn.microsoft.com/library/system.datetime_methods) and [TimeSpan Methods](https://msdn.microsoft.com/library/system.timespan_methods) in the MSDN library.
- Use this cmdlet cautiously when changing the date and time on the computer. The change might
prevent the computer from receiving system-wide events and updates that are triggered by a date or
time. Use the **WhatIf** and **Confirm** parameters to avoid errors.
- You can use standard .NET methods with the **DateTime** and **TimeSpan** objects used with
`Set-Date`, such as **AddDays**, **AddMonths**, and **FromFileTime**. For more information, see
[DateTime Methods](/dotnet/api/system.datetime) and

[TimeSpan Methods](/dotnet/api/system.timespan) in the MSDN library.

## RELATED LINKS

Expand Down
Loading