Script date as string doesn't show time if midnight?

When I use a small VB macro to gather the metadata from the Date Taken field of an image I run into a strange problem.

If an image has a date taken of: 27/07/2018 12:07:30

The Macro duly returns 27/07/2018 12:07:30

However if the Date Taken field is changed to 27/07/2018 00:00:00

The macro returns 27/07/2018

And changing the value from 00:00:00 to 00:00:01 restores things to normal.

The problem is that 00:00:00 is a legitimate time (the Stroke of midnight) for the Date Taken field.

You may ask, how many images do I have taken on the stoke of midnight? And the answer is, as far as I can find - NONE

But the problem becomes bothersome because quite a few images downloaded from the Internet do indeed contain a time of 00:00:00.

It makes string operations on the Date created field a good deal more complicated.

How are you turning the date-time into a string in your script?

This is happening long before I write any data back. This snippet of code reads the datetaken field from an image and deposits it in the firstdate variable.

For Each selItem in clickData.func.sourcetab.selected
_ Set imageData = selItem.Metadata.image_
_ firstdate = imagedata.datetaken_

If I then calculate the length of the of the firstdate variable

I get a result of 19 when the time is NOT set at 00:00:100:

And a result of 10 when it is.

Set the time to 00:00:01 and the result is 19 - which is correct

This shows that 00:00:00 is never in the imagedata.datetaken

Writing the data back is only a problem with a time of 00:00:00 simply because it is not in the variable firstdate to write back .

My routine to write the data back works fine and is:

datery = theyear + "-" + themonth + "-" + theday + " " + mytimery

which has been turned into a string with CStr

where datery is the variable to write back and he other variables contain the broken down data of the year, the month and the day.

mytimery contains the time in the format "hh:mm:ss" turned into a string with CStr

All the data is in string format - not Integer

The routines work perfectly except with a time of 00:00:00, but as the data is not there it is hardly surprising

To get round the problem I have written code to create a random time when the time in the image is 00:00:00.

The question I am asking is why 00:00:00 (midnight) is not picked up by interrogating the metadata of the image datetaken field?

Dates are not string objects. They must be converted to a string via some action, even if it's an implicit one. (e.g. Passing a date object to something that expects a string, or adding it to another variable which is a string.)

It's possible the implicit conversion is using a convention where midnight means it's just a date and no time part is wanted. If so, convert the date to a string explicitly instead of relying on the implicit conversion.

i.e. If just using the date object as if it was a string is not giving you the result you want, then you need to convert it into a string yourself, using an explicit date format.

You can do that in various different ways. If you are using Opus Date objects, they have a Format method which can turn them into strings. If you're using JScript Date objects, you can either convert them into Opus Date objects or use the JScript methods/properties to build a string (which is usually more work).

			var meta = item.metadata;
			var image = meta.image;
			if (image)
			{
				var dateTaken = image.dateTaken;
				var dateString = dateTaken.Format("D#dd-MMM-yy T#HH:mm:ss");
				DOpus.Output(dateString);
			}
1 Like

Many thanks, Leo, that has it sorted.