Ok, convert this!

This topic contains 6 replies, has 2 voices, and was last updated by  Lery 4 years, 2 months ago.

  • Author
    Posts
  • #8518

    Lery
    Participant

    I'm pulling data out of an XML file.  One of those fields is a date.  I reference this with something like $a.b.c.date.  The date looks like this when extracted: Jul 16 16:08:30 +00:00

    Get-Date hates that, so I cannot convert it using Get-Date.  Everything else I've tried has failed miserably.  So I come to you brilliant people with high hopes of converting my screwy date.  Someone is going to give me string manipulation aren't they?

  • #8519

    Poshoholic
    Participant

    This should give you a push in the right direction:

    PS C:\> $month,$day,$hour,$minute,$second,$timezone = 'Jul 16 16:08:30 +00:00' -split '[: ]',6
    PS C:\> $month
    Jul
    PS C:\> $day
    16
    PS C:\> $hour
    16
    PS C:\> $minute
    08
    PS C:\> $second
    30
    PS C:\> $timezone
    +00:00
    
  • #8520

    Lery
    Participant

    Poshoholic,

    After I asked the question I started playing around with string manipulation.  This is what I ran:  $date = $date.Substring(0,16)

    That produced the following string:  Jul 15 16:08:30

    It's better.  I tried what you detailed.  It seems to accomplish the same as mine, yes?

    I would still like to convert Jul 15 16:08:30 to a valid date/time, but I'm not sure I can.  The format is not correct for get-date to convert it.

  • #8523

    Poshoholic
    Participant

    The method I shared yesterday is a little different than what you were doing, because I pulled out the individual pieces so that you could do something with them (like pass them in as parameters to Get-Date). I was in a hurry when I shared that though. Now that I had more time, I was able to find what I wanted to share yesterday. I just couldn't find it in a hurry and it's not something I use often enough that I could recall which method it was.

    [DateTime]::ParseExact($date, 'MMM dd HH:mm:ss zzz', $null)
    

    This will give you exactly what you are looking for.

    If the date format changes, refer to this blog post for the values you can use to specify what the format is:

    http://winpowershell.blogspot.ca/2006/09/systemdatetime-parseexact.html

    Note that this will return an error if you do not provide a string that matches the format identified. For example, if you used hh instead of HH, then you would get an error because the hour is in 24-hour format, not 12-hour format.

    Kirk out.

  • #8524

    Lery
    Participant

    [quote=8523]The method I shared yesterday is a little different than what you were doing, because I pulled out the individual pieces so that you could do something with them (like pass them in as parameters to Get-Date). I was in a hurry when I shared that though. Now that I had more time, I was able to find what I wanted to share yesterday. I just couldn’t find it in a hurry and it’s not something I use often enough that I could recall which method it was.

    [DateTime]::ParseExact($date, ‘MMM dd HH:mm:ss zzz’, $null)
    
    This will give you exactly what you are looking for. If the date format changes, refer to this blog post for the values you can use to specify what the format is: http://winpowershell.blogspot.ca/2006/09/systemdatetime-parseexact.html Note that this will return an error if you do not provide a string that matches the format identified. For example, if you used hh instead of HH, then you would get an error because the hour is in 24-hour format, not 12-hour format. Kirk out. [/quote]
    
    How should I be running this now?
    
    $date = $month,$day,$hour,$minute,$second,$timezone = 'Jul 16 16:08:30 +00:00' -split '[: ]',6
    
    [DateTime]::ParseExact($date, ‘MMM dd HH:mm:ss zzz’, $null)
    
    Does not seem right.  I haven't had my coffee yet, and my brain is not working at 100% 🙂
    
    Ok, I got it.  This works nicely:
    
    $date = 'Jul 16 16:08:30 +00:00'
    
    [DateTime]::ParseExact($date, ‘MMM dd HH:mm:ss zzz’, $null)
  • #8525

    Poshoholic
    Participant

    Yup, you got it, you don't need to do manual parsing this way. The ParseExact static method does that work for you.

  • #8529

    Lery
    Participant

    [quote=8525]Yup, you got it, you don’t need to do manual parsing this way. The ParseExact static method does that work for you.[/quote]

     

    Thanks Posholic, I appreciate your assistance.

You must be logged in to reply to this topic.