0

enter image description here

enter image description here

I am trying to capture the time from SharePoint list using jquery, but it says Uncaught TypeError: $(...).closest is not a function when I try to store the hour field in a variable and then use it. I have attached both the images for reference.Also wanted to convert the time captured from sharepoint list i.e 02:00 PM to 24 hour format i.e 14:00. Requesting anyone to guide me how to fix this.

1

Try using the following script:

Note: "time" is my date&time field name.

enter image description here

<script src="https://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready( function() {     
    // get time
    $("select[id^='time']").change(function(){
        var hours;
        var minutes;
        //get hours
        var hoursStr = $("select[id^='time'][id$='DateTimeFieldDateHours']").find("option:selected").text();
        minutes = $("select[id^='time'][id$='DateTimeFieldDateMinutes']").find("option:selected").text();
        if(hoursStr.includes("AM")){
            hours = parseInt(hoursStr,10);
        }else{
            hours = parseInt(hoursStr,10)+12;
        }
        console.log(hours+":"+minutes);
    });
});
</script>

Result:

enter image description here

  • Thank you for the script, however I need to get the time captured in another change function. I do not want a fucntion. I just need to capture the hours and mins and convert into 24 hour time which is already selected. – Prashanth Edige Sep 20 '18 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.