2

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
1
  • 2
    User input is [string] (unless you cast it to something else). Commented Jul 11, 2015 at 10:53

1 Answer 1

4
$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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.