Author Posts

May 16, 2013 at 5:03 am

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

 

May 16, 2013 at 6:16 am

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

 

May 16, 2013 at 7:00 am

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

 

Thanks again

May 16, 2013 at 8:25 am

testing if i can reply since i can't seem to post. 🙁

May 16, 2013 at 8:33 am

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

May 17, 2013 at 5:56 am

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

May 17, 2013 at 8:39 am

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.

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

May 19, 2013 at 11:59 pm

anyone with any more thoughts 🙁

May 28, 2013 at 6:43 am

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

May 28, 2013 at 9:58 am

 Brute force:

$Date = "16/06/2013"
$Date = $Date.Split('/')
$Date = "{0}/{1}/{2}" -f $Date[1],$Date[0],$Date[2]
$Date
05/16/2013

May 29, 2013 at 12:30 am

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

May 29, 2013 at 12:31 am

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?

May 29, 2013 at 10:05 am

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.

May 30, 2013 at 4:08 am

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 🙂

November 19, 2013 at 10:54 pm

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