Tap Forms – Organizer Database App for Mac, iPhone, and iPad › Forums › Script Talk › Combining Date & Time Info
November 5, 2019 at 7:44 AM #37798
I 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.
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
I should clarify. Using
OK, 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
getFullYear(dateID). The joys of parsing in different environments!November 5, 2019 at 9:27 AM #37806
This 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
Yup! Thank you, Daniel.November 5, 2019 at 10:09 AM #37813
Actually, Daniel, a couple of things about your script.
1. I think you need to use
getDay(), 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
You must be logged in to reply to this topic.