# Date conversion

**URL:** <https://forums.powershell.org/t/date-conversion/7138>\
**Category:** PowerShell Help\
**Created:** [September 6, 2016, 5:53am UTC](https://forums.powershell.org/t/date-conversion/7138 "2016-09-06T05:53:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 5:53am UTC](https://forums.powershell.org/t/date-conversion/7138/1 "2016-09-06T05:53:38Z")

</div>

Hi All

again experimenting with Sapien, but this is likely a general question so thought i would try here 🙂

i am using a gui & some date selection, now my issue iss the data selection adds a time to the end which i cant use for the exchange dumpster command (as below in the $start)

05 September 2016 00:00:00

i realise there is quite a bit of uneeded code here but was just trying to see why the listbox would show a short date and then pass a longer format to the command line. Appreciate any help

```
$formDumpsterRestore_Load={
	#TODO: Initialize Form Controls here
	
	Set-ExecutionPolicy -ExecutionPolicy Unrestricted
	$Username = "global\mark.prior"
	$Password = ConvertTo-SecureString "xxxxxxxxx" -AsPlainText -Force
	$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
	
	$SESJA_EX = New-PSSession -Credential $cred -ConnectionUri http://eurxhub02/powershell -ConfigurationName microsoft.exchange
	Import-PSSession $SESJA_EX
	
}

$labelDateTo_Click={
	#TODO: Place custom script here
	
}

$labelMailbox_Click={
	#TODO: Place custom script here
	
}

$monthcalendar1_DateChanged=[System.Windows.Forms.DateRangeEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DateRangeEventArgs]
	#TODO: Place custom script here
	
}

$monthcalendar2_DateChanged = [System.Windows.Forms.DateRangeEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.DateRangeEventArgs]
	#TODO: Place custom script here
	
}

$Mailbox_TextChanged={
	#TODO: Place custom script here
	
}

$buttonRestore_Click={
	$this.enabled = $false
	
	
	Set-ExecutionPolicy -ExecutionPolicy Unrestricted
	
	$testpath = Get-mailbox $Mailbox.Text
	
	if ($testpath)
	{
		$collection = $Mailbox.Text, $monthcalendar1.SelectionStart, $monthcalendar2.SelectionEnd, $combobox1.Text
		$Start = $monthcalendar1.SelectionStart
		$End = $monthcalendar2.SelectionEnd
		
		$Start |Out-File c:\date.txt
		
		Search-Mailbox $mailbox.text -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $combobox1.Text"$start..$End" -TargetFolder Inbox
		
		Load-ListBox $listBox $collection
		
		
	}
	
	Else
	{
		$empty = "Mailbox Not Found"
		
		Load-ListBox $listBox $empty
	}
	
	#Remove-PSSession $sesja_ex
	
	
	$this.enabled = $true
	
}

$statusbar1_PanelClick=[System.Windows.Forms.StatusBarPanelClickEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.StatusBarPanelClickEventArgs]
	#TODO: Place custom script here
	
}

#region Control Helper Functions
function Load-ComboBox 
{

	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.ComboBox]$ComboBox,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Items,
	    [Parameter(Mandatory=$false)]
		[string]$DisplayMember,
		[switch]$Append
	)
	
	if(-not $Append)
	{
		$ComboBox.Items.Clear()	
	}
	
	if($Items -is [Object[]])
	{
		$ComboBox.Items.AddRange($Items)
	}
	elseif ($Items -is [System.Collections.IEnumerable])
	{
		$ComboBox.BeginUpdate()
		foreach($obj in $Items)
		{
			$ComboBox.Items.Add($obj)	
		}
		$ComboBox.EndUpdate()
	}
	else
	{
		$ComboBox.Items.Add($Items)	
	}

	$ComboBox.DisplayMember = $DisplayMember	
}

function Load-ListBox 
{

	Param (
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.ListBox]$ListBox,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Items,
	    [Parameter(Mandatory=$false)]
		[string]$DisplayMember,
		[switch]$Append
	)
	
	if(-not $Append)
	{
		$listBox.Items.Clear()	
	}
	
	if($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection])
	{
		$listBox.Items.AddRange($Items)
	}
	elseif ($Items -is [System.Collections.IEnumerable])
	{
		$listBox.BeginUpdate()
		foreach($obj in $Items)
		{
			$listBox.Items.Add($obj)
		}
		$listBox.EndUpdate()
	}
	else
	{
		$listBox.Items.Add($Items)	
	}

	$listBox.DisplayMember = $DisplayMember	
}
#endregion

$textbox1_TextChanged={
	#TODO: Place custom script here
	
}

$combobox1_SelectedIndexChanged={
	#TODO: Place custom script here
	
}
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 6, 2016, 6:50am UTC](https://forums.powershell.org/t/date-conversion/7138/2 "2016-09-06T06:50:07Z")

</div>

Manipulating the date to the format you want is simple. You just need to use Get-Date to convert the string into a datetime object and then format the date to what you want to do. Take a look at [Windows PowerShell Tip of the Week: Formatting Dates and Time](https://technet.microsoft.com/en-us/library/ee692801.aspx). Here is a small example:

```
$startDate = "2/2/2015 8:00:00"
$shortDate = Get-Date $startDate -Format d
```

Output:

```
2/2/2015
```

A quick edit. As long as the date is a ‘standard’ datetime format, Get-Date will parse it. If you get funky dates from databases as non-standard string formats, you have to do manual formatting, but the format you posted does parse properly with Get-Date

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 6:51am UTC](https://forums.powershell.org/t/date-conversion/7138/3 "2016-09-06T06:51:45Z")

</div>

Thanks Rob, much appreciated

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 7:43am UTC](https://forums.powershell.org/t/date-conversion/7138/4 "2016-09-06T07:43:20Z")

</div>

Hmmm very odd, i can get the command line

```
Search-Mailbox $mailbox.text -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery $combobox1.Text"$start..$End" -TargetFolder Inbox
```

to work in powershell but not in sapien, Error below

WARNING: The names of some imported commands from the module ‘tmp\_riytoazx.0cw’ include unapproved verbs that might make them less discoverable. To find the commands with unapproved verbs, run the Import-Module command again with the Verbose parameter. For a list of approved verbs, type Get-Verb.  
ERROR: A positional parameter cannot be found that accepts argument ‘04/09/2016…06/09/2016’.  
ERROR: + CategoryInfo : InvalidArgument: (🙂 [Search-Mailbox], ParameterBindingException  
ERROR: + FullyQualifiedErrorId : PositionalParameterNotFound,Search-Mailbox  
ERROR: + PSComputerName : eurxhub02  
ERROR:

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 7:55am UTC](https://forums.powershell.org/t/date-conversion/7138/5 "2016-09-06T07:55:13Z")

</div>

must be something to do with the way im passing info as manually entering

Search-Mailbox mark.prior -SearchDumpsterOnly -TargetMailbox emea-restoreditems -SearchQuery received:“04/09/2016…06/09/2016” -TargetFolder Inbox

works in sapien

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [September 6, 2016, 8:05am UTC](https://forums.powershell.org/t/date-conversion/7138/6 "2016-09-06T08:05:58Z")

</div>

Probably has something to do with passing datetime objects into your search query. Try with strings.

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 8:25am UTC](https://forums.powershell.org/t/date-conversion/7138/7 "2016-09-06T08:25:27Z")

</div>

yep sorted, converted the whole command to a string and its working

Dan another Q you maybe able to help with, how would i get the GUI to display a progress bar (displayng % of command complete or left to go?)

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 6, 2016, 8:26am UTC](https://forums.powershell.org/t/date-conversion/7138/8 "2016-09-06T08:26:13Z")

</div>

The query should be passed as a string, such as this:

```
-SearchQuery 'received:"04/09/2016..06/09/2016"'
```

Also, consider using a splat to pass parameters to your commmand such as this:

```
$startDate = "01 September 2016 00:00:00"
$endDate = "05 September 2016 00:00:00"

$mailboxParams = @{
    Identity = "Mark.Prior"
    TargetMailbox = "emea-restoreditems"
    SearchQuery = 'received:"{0}..{1}"' -f (Get-Date $startDate -f d), (Get-Date $startDate -f d)
    TargetFolder = "Inbox"
    SearchDumpsterOnly=$true
}

$results = Search-Mailbox @mailboxParams
```

Keeps the command clean and you can add and remove command items dynamically with .Remove(“TargetFolder”) or .Add(“SearchDumpsterOnly”, $true).

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [September 6, 2016, 8:33am UTC](https://forums.powershell.org/t/date-conversion/7138/9 "2016-09-06T08:33:20Z")

</div>

Thanks Rob, think i will give splatting a go with this

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [September 6, 2016, 8:35am UTC](https://forums.powershell.org/t/date-conversion/7138/10 "2016-09-06T08:35:10Z")

</div>

$.02 I’m not a fan of the month calendar control. The Datetimepicker is much more flexible.

```
$datetimepicker1_ValueChanged = {
	
	$textbox1.Text = $dateTimePicker1.Value.ToString("yyyy/MM/dd")
	$datetimepicker2.MinDate = $datetimepicker1.Value.AddDays(1)
	
}

$datetimepicker2_ValueChanged = {
	
	$textbox2.Text = $datetimepicker2.Value.ToString("yyyy/MM/dd")
	
}
```

---

<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:40pm UTC](https://forums.powershell.org/t/date-conversion/7138/11 "2024-05-16T20:40:38Z")

</div>


