Combining Date & Time Info

Viewing 6 reply threads
  • Author
    Posts
  • November 5, 2019 at 7:44 AM #37798

    Martin Inchley
    Participant

    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.

    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 Inchley
    Participant

    I 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 Inchley
    Participant

    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 dateID.getFullYear() and not getFullYear(dateID). The joys of parsing in different environments!

    November 5, 2019 at 9:27 AM #37806

    Daniel Leu
    Participant

    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

    Martin Inchley
    Participant

    Yup! Thank you, Daniel.

    November 5, 2019 at 10:09 AM #37813

    Martin Inchley
    Participant

    Actually, Daniel, a couple of things about your script.
    1. I think you need to use getDate() rather than 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

    Daniel Leu
    Participant

    Martin, 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 :)

Viewing 6 reply threads

You must be logged in to reply to this topic.