I can’t get my head around this problem. I’m developing a verification app where a user takes a selfie and then uploads it to my server (node.js express.js formidable.js) via a html form where it goes through a number of tests. One of the screening tests is that the selfie photo must have been taken less than 120 seconds before it is uploaded.
I have the photo created date from the photo’s exif metadata. It is in the format "2023:09:03 16:48:55". This is the local time on the device.
I have the uploaded time, set by setting a form field’s value to new Date() on submit:
<form action="/selfie" enctype="multipart/form-data" method="post" name="fselfie">
<input type="file" name="selfie" multiple="false" />
<input type="hidden" name="lt" id="lt" value="" />
<!--ADD FIELDS-->
<input type="submit" value="Upload" onclick="document.fselfie.lt.value = new Date();" />
</form>
Thee user is using the same device to take the photo and upload it. The field comes back in the format "Sun Sep 03 2023 16:49:12 GMT+1000 (Australian Eastern Standard Time)".
How can I calculate the difference in seconds between these two dates?
function parseDate(s) {
var b = s.split(/D/);
return new Date(b[0], b[1] - 1, b[2], b[3], b[4], b[5]);
}
console.log(exifData.exif.CreateDate);
CreateDate = parseDate(exifData.exif.CreateDate);
console.log(CreateDate);
console.log(fields.lt[0]);
submitDate = new Date(fields.lt[0]);
console.log(submitDate);
age = (submitDate - CreateDate) / (1000 * 60);
console.log(age);
/*
2023:09:03 16:48:55
2023-09-03T16:48:55.000Z
Sun Sep 03 2023 16:49:12 GMT+1000 (Australian Eastern Standard Time)
2023-09-03T06:49:12.000Z
-599.7166666666667
*/
As you can see, I expect about 17 seconds difference but the calculation is about -600 seconds, because of the 10 hour time zone offset. How can I solve this?
3
Answers
Your the create date looks like it is in GMT(UTC+0) since it says 00z, while the
Date()
function is creating a time in the Australian time zone. So the problem is in the difference of time zones; your photo is stored as UTC, while the JS is using your system time zone meaning you need to convert it to UTC to be able to do the math correctly.It is always a good idea to be weary of time zones when you are dealing with times and dates. with that said, here are some solutions:
use the
.toLocaleString()
function to convert the string to a UTC formatted string. it could look something like this:this is functionally equivalent to using the
toUTCString()
method, which would look like this:The final solution is to just specify that the submit date should be in UTC when creating it, which looks like this in your code:
The difference is not 600seconds but 600 minutes (ie the 10 hours Australian EST is ahead of UTC/GMT).
It seems your backend is running on a machine that is set to UTC (== GMT) timezone, so your
parseDate(exifData.exif.CreateDate)
interprets16:48:55
as UTC time instead of the local time of your user, because the EXIF timestamp is missing timezone information. (EXIF doesn’t support timezone information)The simplest would probably be, to get the UTC offset from your
submitDate
and add this offset to yourcreateDate
Maybe an (admittedly somewhat ugly) string manipulation of the returned date will work?
I have created a small, possibly useful Date library.