Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Combining Date & Time Info
- This topic has 6 replies, 2 voices, and was last updated 5 years ago by Daniel Leu.
-
AuthorPosts
-
November 5, 2019 at 7:44 AM #37798
Martin InchleyParticipantI want to combine the date from a Date field and the time from a Time field into a value that can be pasted into a Date/Time field.
Using
record.getFieldValue(fieldID)
on either field type does not return a JavaScript Date Object, but a string with Date and Time info.Please does anyone know how to extract the year, the month number and the day from a Date field, and the hours and minutes from a Time field?
November 5, 2019 at 8:51 AM #37800
Martin InchleyParticipantI should clarify. Using
record.getFieldValue(fieldID)
returns a date in the standard JavaScript output way. My problem is with extracting year, month, day (etc) values so that I can produce a new date object. I can’t see how to do this.November 5, 2019 at 9:18 AM #37805
Martin InchleyParticipantOK, I think I’ve got it. So here it is for anyone else who might like it, and to avoid having anyone taking trouble to respond.
var date_id = 'fld-3ef5526b645947c89317b8272dffd659'; var dateID = record.getFieldValue(date_id); var thisYear = dateID.getFullYear();
My problem was getting used to writing
dateID.getFullYear()
and notgetFullYear(dateID)
. The joys of parsing in different environments!November 5, 2019 at 9:27 AM #37806
Daniel LeuParticipantThis works for me:
var dateId = 'fld-403c042812584c0184c458d221b3515b'; var date = record.getFieldValue(dateId); var m = date.getMonth(); var d = date.getDay(); var y = date.getFullYear(); console.log(date.getMonth()); console.log(date.getDay()); console.log(date.getFullYear()); var d = new Date(y, d+1, m); console.log(d);
November 5, 2019 at 9:37 AM #37809
Martin InchleyParticipantYup! Thank you, Daniel.
November 5, 2019 at 10:09 AM #37813
Martin InchleyParticipantActually, Daniel, a couple of things about your script.
1. I think you need to usegetDate()
rather thangetDay()
, in order to get the day of the month, and not the day of the week.
2. In your final calculation, I think d and m are round the wrong way. Also the w3schools JS tutorial says to be alert for the need to add 1 to the month, because January = 0. But in my script, it works without it.var date_and_time_id = 'fld-a74f18a96d4c42489a40a367f54f45ee'; var date_id = 'fld-3ef5526b645947c89317b8272dffd659'; var time_id = 'fld-95075cb1f5604be59afbc73094591e7a'; var date = record.getFieldValue('fld-3ef5526b645947c89317b8272dffd659'); var time = record.getFieldValue('fld-95075cb1f5604be59afbc73094591e7a'); var m = date.getMonth(); var d = date.getDate(); var y = date.getFullYear(); var h = time.getHours(); var mins = time.getMinutes(); var compDate = new Date(y, m, d, h+1, mins) record.setFieldValue(date_and_time_id, compDate); form.saveAllChanges(); console.log(compDate)
However, I do currently have to add an hour to the hours, because of the problem I flagged in another thread, to which no one has yet come up with an answer.
November 5, 2019 at 10:26 AM #37815
Daniel LeuParticipantMartin, the purpose of my script was just to show that you can get access to the field values using Javascript’s Date methods. I posted my response to your original question before I received your progress update :)
-
AuthorPosts
You must be logged in to reply to this topic.