Re-arranging date format

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

 

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

 

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

 

Thanks again

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

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.

 

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

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

 

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
System.Windows.Forms.RichTextBox, Text: 123456
System.Windows.Forms.RichTextBox, Text: 987654

 

So the date is in the wrong format ?!?

 

Any help would be great

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.

anyone with any more thoughts :frowning:

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

 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

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

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?

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.

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.

 

So the code that I have used is -

 

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

 

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

[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!