Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Scripting Time fields
- This topic has 15 replies, 3 voices, and was last updated 5 years, 1 month ago by Daniel Leu.
-
AuthorPosts
-
October 30, 2019 at 4:37 PM #37585
Martin InchleyParticipantI need to manipulate Time data in scripts. One problem is that when I use the JavaScript getHours() function, it subtracts an hour from the value in the field.
var thisTime = record.getFieldValue('fld-95075cb1f5604be59afbc73094591e7a'); var thisHour = thisTime.getHours(); var thisMinute = thisTime.getMinutes(); var newTime = thisHour + ":" + thisMinute; console.log(newTime);
The field is showing 00:14. The console result is 23:14.
I’m in a GMT zone, and the Mac’s Date & Time settings are on auto.
Does anyone have an explanation / solution, please?
October 31, 2019 at 7:56 AM #37608
Daniel LeuParticipantI can’t reproduce this here. Does
console.log(thisTime)
report 00:14?October 31, 2019 at 10:32 AM #37615
Martin InchleyParticipantDaniel,
I should, of course, have checked this first. The answer is that
console.log(thisTime)
reports “Wed Dec 31 1969 23:14:00 GMT+0000 (GMT)”. So the issue is with the first step (extracting the data from the Time field), not with the getHours() function.To be sure, I’ve just created a completely new Form, with one record, one field, and one script:
var thisTime = record.getFieldValue('fld-7c2e0886dfa54aa59ffb75012ddd49b1'); console.log(thisTime)
I used the time tool attached to the field to enter the current time (17:22:50). Running the script output Thu Jan 01 1970 16:22:50 GMT+0000 (GMT) to the console.
October 31, 2019 at 11:42 AM #37618
BrendanKeymasterTime fields always set the date portion to 0 time, which is January 1, 1970. But your time zone can definitely change how that appears.
When I added a script to get the time, this is what I get when I log to the console:
Thu Jan 01 1970 12:41:38 GMT-0700 (MST)
I had entered
12:41:38 PM
into the field.The output of your script:
function Time_Script() { var time = record.getFieldValue('fld-58b1679f309d4e848f3aaf8f1d22da45'); if (time) { console.log(time); var thisHour = time.getHours(); var thisMinute = time.getMinutes(); var newTime = thisHour + ":" + thisMinute; console.log(newTime); } } Time_Script();
for me was:
Thu Jan 01 1970 12:41:38 GMT-0700 (MST) 12:41
So the same time values.
October 31, 2019 at 1:10 PM #37620
Martin InchleyParticipantThanks, Brendan.
I’ve just created a new document – one form, one (Time) field, one script
I used the field time tool to enter the current time (20:06:26).
Running the script gave me a console output of “Thu Jan 01 1970 19:06:26 GMT+0000 (GMT)
“, which is one hour before the field value.Document attached. I’d really like to know what’s going on!
Attachments:
You must be logged in to view attached files.October 31, 2019 at 3:09 PM #37626
Daniel LeuParticipantIt shows 11:06:26 AM for me and the script reports Thu Jan 01 1970 11:06:26 GMT-0800 (PST).
At a first glance this is okay, but the console reports PST instead of PDT. PDT is UTC -7. Now I’m confused too…
November 1, 2019 at 2:07 AM #37637
BrendanKeymasterI get
Thu Jan 01 1970 02:53:01 GMT-0700 (MST)
in the JS console when I put in my current time.But ya, when I type in
date
in the Terminal app, I getFri 1 Nov 2019 02:52:36 MDT
. Not sure why there’s a discrepancy. They’re the same time though, but one says MST and the other MDT. Well, the same other than the date portion. But that happens because I set all time values to Jan 1, 1970 so that you can compare one time value with another and know they are on the same day.Ah… but if you change it to a Date & Time field, I get MDT:
Fri Nov 01 2019 02:55:36 GMT-0600 (MDT)
I don’t know why this happens. Same time, just MST vs. MDT for me.
November 1, 2019 at 4:53 AM #37639
Martin InchleyParticipantI’m in the UK, and my Mac’s Date/Time is set to auto, which means GMT currently.
Getting the time calculation right in this database is crucial, because the info will be exported to tell musicians what time to turn up for a gig! I look forward to seeing how you folks resolve these problems (opportunities!).
So, related to this question – in fact the reason why I went down this path in the first place – is the issue of how you combine info from a Time field and a Date field to produce a Date
object that can be entered into a Date & Time Field. I’ve tried all sorts of JavaScript instructions, but TapForms doesn’t recognise any of the results as a valid Date-Time that can be used to set a field. Is there a simple method? – something I’m missing, please.November 5, 2019 at 7:54 AM #37799
Martin InchleyParticipantAny progress, please, on the anomalies highlighted in this thread?
TF subtracts 1 hour from the result when I extract the time from a Time field with
record.getFieldValue()
.Daniel and Brendan reported an anomaly with the Time Zone. For me, in the UK, it’s as though TF thinks the time in the originating field is British Summer Time, and so has to subtract an hour to give me the GMT equivalent. I can assure you it is currently definitely not summer in the UK!
November 6, 2019 at 2:38 AM #37847
BrendanKeymasterI’m not really sure why this is happening unfortunately. I’m just using the standard Apple functions for getting the time.
The code to do this is:
+ (NSDate *)exactTimeOnlyFromDate:(NSDate *)date { if (!date) { return nil; } NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger calendarUnits = NSCalendarUnitEra | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; NSDateComponents *dateComps = [calendar components:calendarUnits fromDate:date]; calendar.timeZone = [NSTimeZone systemTimeZone]; [dateComps setYear:1970]; [dateComps setMonth:1]; [dateComps setDay:1]; NSDate *timeOnly = [calendar dateFromComponents:dateComps]; return timeOnly; }
The time zone passed in to the calendar is whatever the system’s time zone is.
Have you tried using a Date & Time field instead of a Time field to see if that makes a difference for you?
November 6, 2019 at 4:15 AM #37850
Martin InchleyParticipantThanks, Brendan.
The effect is not there with a Date field, nor with a Date & Time field, only with a Time field.
Extracting the hour from a Date or a Date/Time field gives the true value.November 6, 2019 at 1:57 PM #37871
Martin InchleyParticipantI got my son to test this on his Mac. He gets the same result: Time field reads 10:15 p.m. Extracting the field value and sending it to the console gives “Thu Jan 01 1970 21:15:29 GMT+0000 (GMT)”. I.e 1 hour has been subtracted in the extraction.
November 12, 2019 at 9:18 AM #38012
Martin InchleyParticipantPlease can I bump this, as the ability to call a correct time of arrival is crucial. Here’s a little extra info:
Context: I’m in a GMT timezone, Mac set to auto, all normal, currently winter so not British Summer Time.
Time Field: “Time of Arrival”. I want to extract the time from this field. It is currently set to 3.50 p.m.
If I use this script:
var time_of_arrival_id = 'fld-4957bec764cf480884d3c7f8cce5a271'; var thisTime = record.getFieldValue(time_of_arrival_id); console.log(thisTime);
the console reports: Thu Jan 01 1970 14:50:30 GMT+0000 (GMT).
I.e it’s subtracted an hour from the time in the field.But if I add the JS function
.toLocaleTimeString()
to the console.log line:var time_of_arrival_id = 'fld-4957bec764cf480884d3c7f8cce5a271'; var thisTime = record.getFieldValue(time_of_arrival_id); console.log(thisTime.toLocaleTimeString());
the console reports 15:50:30, which is correct.
I really need to know what’s going on, so that I can control it. Earlier in this thread, both Daniel and Brendan reported anomalies, but no one yet seems to have an explanation or a solution.
November 12, 2019 at 10:55 AM #38014
Daniel LeuParticipantSetting my computer to GMT gives me this result:
Tue Nov 12 2019 18:00:30 GMT+0000 (GMT) 6:00:30 PM
Don’t know why this is different for you. Just for curiosity, what do you get with
console.log(thisTime.toTimeString());
?Looks like whatever abnormality you see in your system,
thisTime.toLocaleTimeString()
fixes it.November 12, 2019 at 11:46 AM #38015
Martin InchleyParticipantUsing
console.log(thisTime.toTimeString());
gives me:14:50:30 GMT+0000 (GMT) – subtracting one hour.
I could understand there being a coding error generating the wrong time string. But how would the .toLocaleString() method correct that?
As reported above, Daniel, I got my son to test this as well, and he got the same anomaly as me.
What version of TapForms are you using? I’m on 5.3.8, build 1821.November 12, 2019 at 1:47 PM #38016
Daniel LeuParticipantI’m using the same version.
-
AuthorPosts
You must be logged in to reply to this topic.