IsTime Function

Dreamweaver

Well-known member
Local time
Today, 14:48
Joined
Nov 28, 2005
Messages
2,467
I have been struggling with this for hours and after trying a number of functions from the web which wouldn't give me the correct answer I went back to basics

To find out if the time part of a date is a real time use this function

Code:
Public Function IsTime(dat As String) As Boolean
IsTime = Left(dat, 2) <> "00"
End Function
Now the key is to convert the time part of the date into a string like
DT is the date/Time value

IsTime(CStr(Format(DT, "Short Time")))

Some history
I have built an import engine that has to format a file name which can be like
Code:
2014-09-08 18.16.11
20140108_235915
20140131_211803(1)
IMG_20130916_155007
20140305
Come into my world
As you can see all except the last file name can convert to a date and all except the 20140305 a time.

what my engine does is if the filename doesn't contain a name it gets the date and time created from the file properties I can't get to the date taken maybe somebody will know how without using another program.
D= Return from file properties function which returns the date/time created
DT= Return from the file name format conversion

so the test for date and time looks like
Code:
If year(DT) > 1900 Then
        !DateTaken = Format(DT, "Short Date")
    Else
        !DateTaken = Format(D, "Short Date")
    End If
    If IsTime(CStr(Format(DT, "Short Time"))) Then
        !TimeTaken = Format(DT, "Short Time")
    Else
        !TimeTaken = Format(D, "Short Time")
    End If
 
Mick
I've moved this out of the repository as a simple test shows its not working as you described

I did this:
Code:
Public Function IsTime(dat As String) As Boolean
IsTime = Left(dat, 2) <> "00"
End Function

Sub testistime()

Dim dt As String

'dt = "2014-09-08 18.16.11"
'dt = "20140108_235915X"
'dt = "20140131_211803(1)"
'dt = "IMG_20130916_155007"
'dt = "20140305"
dt = "Come into my world"
   Debug.Print IsTime(CStr(Format(dt, "Short Time")))
End Sub

All of those return true including the 'pure' text string
Whereas a valid time string such as dt="00:45:23" returns false

Sorry but it doesn't work as described
 
Mick
I've moved this out of the repository as a simple test shows its not working as you described

I did this:
Code:
Public Function IsTime(dat As String) As Boolean
IsTime = Left(dat, 2) <> "00"
End Function

Sub testistime()

Dim dt As String

'dt = "2014-09-08 18.16.11"
'dt = "20140108_235915X"
'dt = "20140131_211803(1)"
'dt = "IMG_20130916_155007"
'dt = "20140305"
dt = "Come into my world"
   Debug.Print IsTime(CStr(Format(dt, "Short Time")))
End Sub
All of those return true including the 'pure' text string
Whereas a valid time string such as dt="00:45:23" returns false

Sorry but it doesn't work as described


Sorry isladogs I added this part for a reason


Code:
If year(DT) > 1900 Then
        !DateTaken = Format(DT, "Short Date")
    Else
        !DateTaken = Format(D, "Short Date")
    End If
    If IsTime(CStr(Format(DT, "Short Time"))) Then
        !TimeTaken = Format(DT, "Short Time")
    Else
        !TimeTaken = Format(D, "Short Time")
     End If
The values are returned from a function I'm now going to include
Code:
    DT = GetDatePart(StrCode)
Code:
Public Function GetDatePart(StrDT As String) As Variant
Dim DT As Date
Dim Y As Long, M As Integer, D As String
Dim Hou As Double, Min As Double, Sec As Integer
Dim StrTM As String

On Error Resume Next
'Format Name
If InStr(StrDT, "IMG_") <> 0 Then StrDT = Replace(StrDT, "IMG_", "", 1)
If InStr(StrDT, " ") <> 0 Then StrDT = Replace(StrDT, " ", "_", 1)
If InStr(StrDT, ".") <> 0 Then StrDT = Replace(StrDT, ".", "", 1)
If InStr(StrDT, "-") <> 0 Then StrDT = Replace(StrDT, "-", "", 1)
    StrTM = StrDT
    If InStr(StrDT, "_") = 9 Then
        StrDT = Left(StrDT, InStr(StrDT, "_") - 1)
            If IsNumeric(StrDT) Then
                Y = Left(StrDT, 4)
                M = Mid(StrDT, 5, Len(StrDT) - 6)
                D = Mid(StrDT, 7, Len(StrDT))
            End If
        StrTM = Right(StrTM, Len(StrTM) - InStr(StrTM, "_"))
        StrTM = Left(StrTM, 6)
            Hou = Left(StrTM, 2)
            Min = Mid(StrTM, Len(StrTM) - 3, Len(StrTM) - 4)
            Sec = Right(StrTM, Len(StrTM) - 4)
            GetDatePart = DateSerial(Y, M, D) & " " & TimeSerial(Hou, Min, Sec)
    End If
    
End Function
I've run it against a test dataset of 900 records And works perfectly

I would hope somebody could clean it up as I'm sure there are a lot better than me thanks
mick
 
OK it wasn't completely clear from the original post.
I'll test that extra code later.
My point still applies.
Your IsTime function isn't reliably testing whether a string includes a valid time

However, you seem to be suggesting you just want the date/time created or modified property. If so, that's easily done.
For example, look at these posts:
https://www.access-programmers.co.uk/forums/showpost.php?p=1645498&postcount=5
https://www.access-programmers.co.uk/forums/showpost.php?p=1537880&postcount=1

Hope that helps
 
OK it wasn't completely clear from the original post.
I'll test that extra code later.
My point still applies.
Your IsTime function isn't reliably testing whether a string includes a valid time

However, you seem to be suggesting you just want the date/time created or modified property. If so, that's easily done.
For example, look at these posts:
https://www.access-programmers.co.uk/forums/showpost.php?p=1645498&postcount=5
https://www.access-programmers.co.uk/forums/showpost.php?p=1537880&postcount=1

Hope that helps


Hi I can get the date time created without a problem the time I'm after is the time the picture was taken I can get it if I reference a third party program but I really don't want to do that for this project.
you may notice a lot of the pictures are taken with one phone or another which addeds the date/time the picture was taken to the filename hence what I'm doing.


Just one other note I did state the below:
D= Return from file properties function which returns the date/time created
DT= Return from the file name format conversion


Maybe I should have added the function as well but corrected now


mick
 
In case it helps, have a look at this function taken from https://bytes.com/topic/access/answers/197276-istime-function

Code:
Public Function IsTime(dat As Variant) As Boolean
'handle null values
If IsNull(dat) Then
    IsTime = False
'handle text-values, or values that would otherwise not convert to a Date
ElseIf Not IsDate(dat) Then
    IsTime = False
'handle valid 'date' values, but ensure the date part of the date/time value
'has not been entered. We only want the 'time' part of the date/time value.
ElseIf Fix(CDbl(CDate(dat))) <> 0# Then
    IsTime = False
'ok, so it's a valid date, and it is between 0 and 24 hours, i.e. it 's a time value.
Else
    IsTime = True
End If
End Function

As stated in the code, the date part needs to be stripped out first but it does reliably check for valid times...though that doesn't appear to be what you actually want here
 
In case it helps, have a look at this function taken from https://bytes.com/topic/access/answers/197276-istime-function

Code:
Public Function IsTime(dat As Variant) As Boolean
'handle null values
If IsNull(dat) Then
    IsTime = False
'handle text-values, or values that would otherwise not convert to a Date
ElseIf Not IsDate(dat) Then
    IsTime = False
'handle valid 'date' values, but ensure the date part of the date/time value
'has not been entered. We only want the 'time' part of the date/time value.
ElseIf Fix(CDbl(CDate(dat))) <> 0# Then
    IsTime = False
'ok, so it's a valid date, and it is between 0 and 24 hours, i.e. it 's a time value.
Else
    IsTime = True
End If
End Function
As stated in the code, the date part needs to be stripped out first but it does reliably check for valid times...though that doesn't appear to be what you actually want here


Sorry tried that one didn't do what I needed Also tried
http://www.vbforums.com/showthread.php?359445-IsTime-function
https://www.reddit.com/r/excel/comments/28obpn/vba_istime/
https://bytes.com/topic/access/answers/197276-istime-function
Plus a few more lol
Nothing would test correctly until I can up with what i Did my thnking there is if there is no hour then to me it's not a time??
 
I have been futher testing and it will also convert this filename to it's date time
20140719_230707_Android
 
I have been futher testing and it will also convert this filename to it's date time
20140719_230707_Android

What happens with these examples?
20140229_002125_xyz
20141230_252311_abc
19102019_132205_now

The first is an invalid date. The second an invalid time. The last has UK date format for current datetime.
 
20140229_002125_xyz This wont test for a time as my code uses a 24 hour clock there should never be a 00
20141230_252311_abc
19102019_132205_now


You found a few lol
19/08/1911 13:22:05
30/12/2014 31/12/1899 01:23:11
01/03/2014 00:21:25

But I am using files created from a phone which has the correctly formated date and time and testing with a 24 hour time.
If somebody was to rename there files like above then It should revert to the file created except the first I will alter a few files just to play thanks mick


This one give an error 13 20141230_252311_abc so just need to handle that thanks
I'll test the hour value and that should sort that lol
 
Last edited:
Sorry but I didn't understand your last answer.
My first example included a valid time. 00:21:25 is 21 min 25 sec after midnight.

Don't get me wrong.
If your code works for your purposes of images extracted from your phone, that's great.
However, it can't be used reliably as an IsTime function check.
 
This sorts that problem I could set it to 00 but

If Hou > 24 Then Hou = 23
I don't think I'll have that problem but now the codes there I'll leave it thanks mick
 
I could also alter the year test for anything after 1930 Or later as when did images get computerised?
 
I've updated the code so it now checks the year >1940 after asking google the question above


I get for each
20140229_002125_xyz Returns Datevalue =01/03/2014 TimeValue =13:34
The date has been moved one day and the time is now the date created.
19102019_132205_now Returns Datevalue =19/10/2019 TimeValue =13:22
Both are now using the date created
20141230_252311_abc Returns Datevalue =30/12/2014 TimeValue =13:34
The Date is the file name date part but the Time is now the date created as I updated:
If Hou > 24 Then Hou = "00"
 
Sorry but I didn't understand your last answer.
My first example included a valid time. 00:21:25 is 21 min 25 sec after midnight.

Don't get me wrong.
If your code works for your purposes of images extracted from your phone, that's great.
However, it can't be used reliably as an IsTime function check.


You are correct maybe I should have renamed it containsTime as as it looks for a valid hour part of a date/time not sure what to call it now lol


mick
 
To answer the question of when images first got computerized, look up the image types on something like Wikipedia. I don't think you CAN care about when the image was taken. You can only care about when it was digitized and that would depend on when that standard was first created.

For instance, this shows you that JPG images cannot precede about 1986.

https://en.wikipedia.org/wiki/JPEG

It will be up to you to find when each image format was created. However, if you want to filter out "impossibly early" image dates with one "quick-and-dirty" comparison, the advent of the PC is probably a good date. While image compression and reproduction methods existed before that, they were not widespread because people didn't store images on computers. They used microfilm or microfiche.
 
To answer the question of when images first got computerized, look up the image types on something like Wikipedia. I don't think you CAN care about when the image was taken. You can only care about when it was digitized and that would depend on when that standard was first created.

For instance, this shows you that JPG images cannot precede about 1986.

https://en.wikipedia.org/wiki/JPEG

It will be up to you to find when each image format was created. However, if you want to filter out "impossibly early" image dates with one "quick-and-dirty" comparison, the advent of the PC is probably a good date. While image compression and reproduction methods existed before that, they were not widespread because people didn't store images on computers. They used microfilm or microfiche.


Thanks The_Doc_Man I did set it for 1940 in the end but 1986 might be better.
Just run it against A Copy of my main dataset And worked a treat the help Isladogs gave saved 2 errors so thanks their It worked with a dataset of 6000 with the following code
Code:
Public Sub UpdateDates()
Dim P As DAO.Recordset
Dim DT As Date

Set P = CurrentDb.OpenRecordset("SELECT * FROM tblPictureStorage", dbOpenDynaset)
    Do While Not P.EOF
        P.Edit
        DT = GetDatePart(P("PictureCode"))
            If year(DT) > 1940 Then
                P("DateTaken") = Format(DT, "Short Date")
            End If
            If IsTime(CStr(Format(DT, "Short Time"))) Then
                P("TimeTaken") = Format(DT, "Short Time")
            End If
        P.Update
        P.MoveNext
    Loop
End Sub


Thanks for the help


mick
 
For digitised pictures I'm still unclear why you don't just use the Date Created property for the file. Much less code and utterly reliable...
Did you look at the examples I suggested in one of my first replies? Particularly the folder image viewer where I get the date created, date modified, file size and image dimensions from the file properties
 
For digitised pictures I'm still unclear why you don't just use the Date Created property for the file. Much less code and utterly reliable...
Did you look at the examples I suggested in one of my first replies? Particularly the folder image viewer where I get the date created, date modified, file size and image dimensions from the file properties


No I didn't but will as the images dimensions are one I have been looking into so thanks for the shortcut.


I don't want to use the dates you said as I have found most of the pictures I have have a different date taken than the date created.


most of my images are stored on onedrive.


I don't know why but all the images I can get the date taken from the name don't have the same date created even when copying to a new location the date created changes but the date taken doesn't

attachment.php



This is what it's being used for
attachment.php
 

Attachments

  • 2019-10-19 (3).png
    2019-10-19 (3).png
    11.7 KB · Views: 464
  • 2019-10-19 (4).png
    2019-10-19 (4).png
    56.2 KB · Views: 437
Last edited:
I ran your program against the same file in the image of the big picture aand got this please note the dates plus most of the newer picture are png and It doesn't seen to include the date taken?


Edit I was incorrect most of the pictures are jpg so will look at using your code to get the date and mine for ones that don't include the date taken.



attachment.php
 

Attachments

  • 2019-10-19 (5)_LI.jpg
    2019-10-19 (5)_LI.jpg
    20.2 KB · Views: 401
Last edited:

Users who are viewing this thread

Back
Top Bottom