date time conversion

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of 37mm 37mm 8 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #34429
    Profile photo of 37mm
    37mm
    Participant

    I have this value 2016-01-15 19:31:24
    That value is $array[0]
    I convert it by
    $time = [DateTime]::Parse($array[0])
    with the result
    Friday, January 15, 2016 7:31:24 PM

    When I assign Time in a html $body string. I get the initial value. ( 2016-01-15 19:31:24)
    Is there something I am not doing?

    #34435
    Profile photo of Don Jones
    Don Jones
    Keymaster

    No. Internally, it's all stored as a fractional number. In different situations, it'll get formatted differently. You just need to specify the format you want. There are methods of a DateTime object that produce various formats. What you're after is probably a "long date time."

    #34439
    Profile photo of Dan Potter
    Dan Potter
    Participant

    forget the array.

    $time = [datetime]'2016-01-15 19:31:24'

    $time.datetime.gettype()

    #34445
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    PowerShell's format commands behave differently than when a DateTime object is just cast to a string (or when its ToString() method is called with no arguments). If you do "$date", then it's the latter, casting behavior that is used. When you just do $date and let PowerShell display the value, it goes through PowerShell's formatting system.

    Anyhow, this probably does what you're after: "My date is: $($date.ToString('F'))"

    Why 'F'? Got that info from https://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 , and I _constantly_ have to refer to those "Standard Format Strings" pages to remember what's what.

    #34451
    Profile photo of 37mm
    37mm
    Participant
    $time1 = $array1[0]
    $date1 = [DateTime]::Parse($time1)
    $timev1 = $date1.Datetime
    

    Doing it this way allowed me to define the date correctly in the html body. I guess my question is now how to convert to the correct time zone.

    Dave,
    I really appreciate your time and that link is a perma bookmark moving forward!

    Also to everyone thank you for your time and help, I truly appreciate it.

    #34453
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Time zone stuff can be a little bit tricky. First you have to know what your original string represents: "2016-01-15 19:31:24". Is that local time? Zulu time? Something else?

    #34454
    Profile photo of 37mm
    37mm
    Participant

    No, that time is 9 hours ahead. I am on the east coast ( new york, USA) EST time zone

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.