Re-arranging date format

Tagged: 

This topic contains 14 replies, has 6 voices, and was last updated by Profile photo of Vladimír Meier Vladimír Meier 3 years, 7 months ago.

  • Author
    Posts
  • #4927
    Profile photo of chrisuk11
    chrisuk11
    Participant

    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

     

  • #4933
    Profile photo of Jeffrey Smith
    Jeffrey Smith
    Participant

    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

     

    • #11580
      Profile photo of Vladimír Meier
      Vladimír Meier
      Participant

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

  • #4939
    Profile photo of chrisuk11
    chrisuk11
    Participant

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

     

    Thanks again

  • #4950
    Profile photo of ccrews
    ccrews
    Participant

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

  • #4960
    Profile photo of Jeffrey Smith
    Jeffrey Smith
    Participant

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

  • #6609
    Profile photo of chrisuk11
    chrisuk11
    Participant

    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

  • #6613
    Profile photo of Jeffrey Smith
    Jeffrey Smith
    Participant

    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.

  • #6713
    Profile photo of chrisuk11
    chrisuk11
    Participant

    anyone with any more thoughts 🙁

  • #7009
    Profile photo of chrisuk11
    chrisuk11
    Participant

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

  • #7010
    Profile photo of Art Beane
    Art Beane
    Member

     Brute force:

    $Date = "16/06/2013"
    $Date = $Date.Split('/')
    $Date = "{0}/{1}/{2}" -f $Date[1],$Date[0],$Date[2]
    $Date
    05/16/2013
  • #7039
    Profile photo of chrisuk11
    chrisuk11
    Participant

    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

  • #7040
    Profile photo of chrisuk11
    chrisuk11
    Participant

    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?

  • #7052
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    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.

  • #7059
    Profile photo of chrisuk11
    chrisuk11
    Participant

    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 🙂

You must be logged in to reply to this topic.