Results 1 to 11 of 11

Thread: Problem with hours in dates(Resolved)

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Problem with hours in dates(Resolved)

    You can get the hour portion of the time from a date data type using the hour method. However, this returns 5 for 5:00 AM, and 5 for 5:00 PM.

    What technique do you use to get this in 24-hour format? I would like 5 for 5:00 AM, and 17 for 5:00 PM.
    Last edited by Shaggy Hiker; Sep 29th, 2004 at 10:16 AM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    You sure about that? Maybe it's windows setting or something, but this worked fine for me, that is, it returns 17, not 5
    VB Code:
    1. Dim myDate As Date = New Date(2004, 9, 28, 17, 42, 42)
    2.         MessageBox.Show(myDate.Hour)

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I can see why that would work, I kind of thought that if I put in hours in 24 hour format, it would recognize it. However, I want to get the hour out of a file creation time, which is in 12 hour format. It seems like there ought to be a way to take a time in 12 hour format and return it in 24 hour format, but I haven't seen any obvious solution.

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    ok, but you said you were using the "date data type". What's your code look like?

  5. #5

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I don't have it around here, but it is something like this:

    VB Code:
    1. dim dt as date
    2.  
    3. dt=fil.datecreated
    4.  
    5. 'Actually, I just made up that last line, but you get the idea.
    6.  
    7. if dt.hour <5 then
    8. 'Something
    9. end if

    That is pretty contrived, but it covers the idea. dt.Hour will return 3 for either 3:00AM or 3:00PM. This would mean that 3:00 PM would be true in the if statement, when what I want is for it to be false.
    My usual boring signature: Nothing

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Real code would help, but this works fine, that is, the last modified time is 3:49 PM and you see "15" in the message box
    VB Code:
    1. Dim fi As FileInfo = New FileInfo("c:\temp\sleuth-small.jpg")
    2.         MessageBox.Show(fi.LastWriteTime.Hour)

  7. #7

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I'll have to check this, but it sure looks like what I was doing. I didn't get 15, though, I got 3.

    I'll have another look this evening. Maybe there is some environment setting in windows.
    My usual boring signature: Nothing

  8. #8
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    The documentation states that the Hour property is a number between 0 and 23.

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    If (PM) then Hours += 12

  10. #10

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    It's that If (PM) part that is the problem. Is there something that returns that?

    I'll have to look again at the hours, I don't think that is what I got, but it was late. I suppose I might have seen the wrong thing.
    My usual boring signature: Nothing

  11. #11

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Oops, I guess it was 24 hour time. My problem was elsewhere. I guess I was too tired to be writing.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width