# Re-arranging date format

**URL:** <https://forums.powershell.org/t/re-arranging-date-format/64>\
**Category:** PowerShell Help\
**Created:** [May 15, 2013, 11:33pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64 "2013-05-15T23:33:33Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 15, 2013, 11:33pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/1 "2013-05-15T23:33:33Z")

</div>

Hi,

I have the following value generated from a date picker-

16/05/2013 12:57:12

I need to change this to the following (without the time) - MM/DD/YYYY

How would I go about doing this please?

Thanks

&nbsp;

---

<div class="post-metadata">

**Author:** ![jeffrey-smith](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jeffrey-smith](https://forums.powershell.org/u/jeffrey-smith)\
**Post date:** [May 16, 2013, 12:46am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/2 "2013-05-16T00:46:04Z")

</div>

If the datepicker is returning that as a string, you can just split it on the space like this:

```
PS C:\scripts> '16/05/2013 12:57:12'.Split(' ')

16/05/2013
12:57:12

PS C:\scripts> '16/05/2013 12:57:12'.Split(' ') | select -First 1

16/05/2013

PS C:\scripts> ('16/05/2013 12:57:12'.Split(' '))[0]

16/05/2013
```

If it’s a DateTime object, you can just do something like this:

```
PS C:\scripts> get-date $datetimeObject -Format 'dd/MM/yyyy'

16/05/2013
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 16, 2013, 1:30am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/3 "2013-05-16T01:30:31Z")

</div>

thanks. how do I then swap the dd/mm to mm/dd ?

&nbsp;

Thanks again

---

<div class="post-metadata">

**Author:** ![ccrews](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@ccrews](https://forums.powershell.org/u/ccrews)\
**Post date:** [May 16, 2013, 2:55am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/4 "2013-05-16T02:55:40Z")

</div>

testing if i can reply since i can’t seem to post. ☹

---

<div class="post-metadata">

**Author:** ![jeffrey-smith](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jeffrey-smith](https://forums.powershell.org/u/jeffrey-smith)\
**Post date:** [May 16, 2013, 3:03am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/5 "2013-05-16T03:03:11Z")

</div>

Ah, yes. Well, it seems that the [datetime] accelerator is using en-US for its date parsing. So for me, at least, I have to tell it to use a different culture that uses dates in the day/month format.

&nbsp;

```
$culture = New-Object system.globalization.cultureinfo 'en-gb'

$date = [datetime]::parse('16/05/2013', $culture)

get-date $date -format 'MM/dd/yyyy'

05/16/2013
```

Be care to use capital M’s for month (m is minute).

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 17, 2013, 12:26am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/6 "2013-05-17T00:26:53Z")

</div>

I have managed to change the format using insstructions above (thanks). However it is not working when I am doing the following -

Â If($radiobuttonOOOScheduled.Checked)  
Â {  
Â Â $OOOdatetimepickerFrom = Get-Date $OOOdatetimepickerFrom.Value -format ‘MM/dd/yyyy’  
Â Â $OOOdatetimepickerTo = Get-Date $OOOdatetimepickerTo.Value -format ‘MM/dd/yyyy’  
Â Â Set-MailboxAutoReplyConfiguration $OOFEmailAddress.text â€“AutoReplyState Scheduled -StartTime $OOOdatetimepickerFrom -EndTime $OOOdatetimepickerTo -internalMessage $OOONewInternalMessage -ExternalMessage $OOONewExternalMessage  
Â Â Â Â  
Â Â }  
Â  
Â #Write-Host $OOOdatetimepickerFrom  
Â #Write-Host $OOOdatetimepickerTo  
Â Write-Host $OOOdatetimepickerFrom  
Â Write-Host $OOOdatetimepickerTo  
Â Write-Host $OOFEmailAddress.text  
Â Write-Host $OOONewInternalMessage  
Â Write-Host $OOONewExternalMessage

&nbsp;

Output is -

System.Windows.Forms.DateTimePicker, Value: 17/05/2013 14:13:57  
System.Windows.Forms.DateTimePicker, Value: 25/05/2013 14:13:57  
[chollis@hah.co.uk](mailto:chollis@hah.co.uk)  
System.Windows.Forms.RichTextBox, Text: 123456  
System.Windows.Forms.RichTextBox, Text: 987654

&nbsp;

So the date is in the wrong format ?!?

&nbsp;

Any help would be great

---

<div class="post-metadata">

**Author:** ![jeffrey-smith](https://avatars.discourse-cdn.com/v4/letter/j/57b2e6/32.png) [@jeffrey-smith](https://forums.powershell.org/u/jeffrey-smith)\
**Post date:** [May 17, 2013, 3:09am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/7 "2013-05-17T03:09:58Z")

</div>

Now it looks like you’re running into Type issues with interacting with Windows Forms. It looks like the variables are still of WinForms types, instead of the native .NET DateTime. I’m not very experienced with WinForms, but I would recommend using a DIFFERENT variable for the DateTime objects, instead of trying to reuse your current variables.  
\<pre class="lang:ps decode:true "\> $OOOToDate = Get-Date $OOOdatetimepickerTo.Value -format â€˜MM/dd/yyyyâ€™  
etc.

Then see what they look like in your Write-Host debug output, and see whether Set-MailboxAutoReplyConfiguration likes those new variable better.

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 19, 2013, 6:29pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/8 "2013-05-19T18:29:46Z")

</div>

anyone with any more thoughts ☹

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 28, 2013, 1:13am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/9 "2013-05-28T01:13:13Z")

</div>

anymore ideas, as still not able to take a date from the date picker and set the format ☹

---

<div class="post-metadata">

**Author:** ![artb0514](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@artb0514](https://forums.powershell.org/u/artb0514)\
**Post date:** [May 28, 2013, 4:28am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/10 "2013-05-28T04:28:48Z")

</div>

Â Brute force:  
\<pre class=“lang:ps decode:true crayon-selected”\>$Date = “16/06/2013”  
$Date = $Date.Split(‘/’)  
$Date = “{0}/{1}/{2}” -f $Date[1],$Date[0],$Date[2]  
$Date  
05/16/2013

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 28, 2013, 7:00pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/11 "2013-05-28T19:00:42Z")

</div>

Whether I try and split, or try and replace the last 9 digits, I keep getting the following error -

date1 = 29/05/2013 09:24:12  
ERROR: Method invocation failed because [System.DateTime] doesn’t contain a method named ‘substring’.

Code used for the above error -

$Date = $OOOdatetimepickerFrom.value  
Â Write-Host "date1 = " $Date  
Â $OOOdatetimepickerFrom.value = $OOOdatetimepickerFrom.value.substring(0,$OOOdatetimepickerFrom.value.Length-9)  
Â Write-Host "Date = " $OOOdatetimepickerFrom.value

When using the split command error -

ERROR: Unable to index into an object of type System.DateTime.  
Heldesk Tool .1l.pff (66): ERROR: At Line: 66 char: 32  
ERROR: +Â Â Â Â $Date = “{0}/{1}/{2}” -f $Date[\<\<\<\< 1],$Date[0],$Date[2]  
ERROR:Â Â Â Â + CategoryInfoÂ Â Â Â Â Â Â Â Â : InvalidOperation: (1:Int32) , RuntimeException  
ERROR:Â Â Â Â + FullyQualifiedErrorId : CannotIndex

Script used for above error -

$Date = $OOOdatetimepickerFrom.value  
$Date = $Date.split(‘/’)  
$Date = “{0}/{1}/{2}” -f $Date[1],$Date[0],$Date[2]  
Write-Host "Date = " $Date

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 28, 2013, 7:01pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/12 "2013-05-28T19:01:34Z")

</div>

Is there anyway I can stop it being the system object and just convert it to a value? As I assume this would fix the errors above?

---

<div class="post-metadata">

**Author:** ![bob-mccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bob-mccoy/32/650_2.png) [@bob-mccoy](https://forums.powershell.org/u/bob-mccoy)\
**Post date:** [May 29, 2013, 4:35am UTC](https://forums.powershell.org/t/re-arranging-date-format/64/13 "2013-05-29T04:35:05Z")

</div>

It would be helpful if you defined what you meant by “a value” when you say “… just convert it to a value.” So at this point I’m assuming you mean a string.

$culture\_gb = New-Object System.Globalization.CultureInfo ‘en-gb’  
$date = [datetime]::parse(“16/05/2013 12:57:12”, $culture\_gb)  
(Get-Date $date -Format MM/dd/yy).ToString()

Of course you would replace the quoted string next to the parse function with whatever variable you were using from your date picker.

---

<div class="post-metadata">

**Author:** ![chrisuk11](https://avatars.discourse-cdn.com/v4/letter/c/3be4f8/32.png) [@chrisuk11](https://forums.powershell.org/u/chrisuk11)\
**Post date:** [May 29, 2013, 10:38pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/14 "2013-05-29T22:38:40Z")

</div>

Thanks to everyone for their help.

Its a face palm moment on my part. The If statement had a misspelt variablehence it was not processing it.

&nbsp;

So the code that I have used is -

&nbsp;

Â $OOOToDate = Get-Date $OOOdatetimepickerTo.value -format ‘dd/MM/yyyy’  
Â Â $OOOFromDate = Get-Date $OOOdatetimepickerFrom.value -format ‘dd/MM/yyyy’

&nbsp;

Again thanks for help, and sorry for such an error on my part 🙂

---

<div class="post-metadata">

**Author:** ![vladimir-meier](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vladimir-meier/32/896_2.png) [@vladimir-meier](https://forums.powershell.org/u/vladimir-meier)\
**Post date:** [November 19, 2013, 5:24pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/15 "2013-11-19T17:24:38Z")

</div>

[quote=4933]If the datepicker is returning that as a string, you can just split it on the space like this:

```
PS C:\scripts> '16/05/2013 12:57:12'.Split(' ')

16/05/2013
12:57:12

PS C:\scripts> '16/05/2013 12:57:12'.Split(' ') | select -First 1

16/05/2013

PS C:\scripts> ('16/05/2013 12:57:12'.Split(' '))[0]

16/05/2013
```
If itâ€™s a DateTime object, you can just do something like this:
```
PS C:\scripts> get-date $datetimeObject -Format 'dd/MM/yyyy'

16/05/2013
```

[/quote]  
Thanks a lot!

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/re-arranging-date-format/64/16 "2024-05-16T20:52:35Z")

</div>


