charles
charles

Reputation: 297

Converting user input to DateTime value

I have some user input - for example 150715 - it is supposed to be a date, but I have to change the format of int to datetime -format yyMMdd.

I have tried something like this:

$input = Read-Host "Get number (date)"
$input
$input_toDate = [datetime]$input -format yyMMdd

It doesn't work.

Is it possible to create this?

$input_toDate_up = ($input_toDate).AddDays(5) -Format yyMMdd

Upvotes: 2

Views: 1649

Answers (1)

Alexander Obersht
Alexander Obersht

Reputation: 3275

$input = Read-Host "Get number (date)"
$format = "yyMMdd"
$input_toDate_up = [DateTime]::ParseExact($input, $format, $null).AddDays(5).ToString($format)
$input_toDate_up

Input: 150715, output: 150720.

See DateTime.ParseExact reference for more info.

Upvotes: 4

Related Questions