0

Hi Everyone,

Thank you ahead of time for taking a look... Basically, the tables, rows, name and id tags below shown are pulled through by using PHP/cURL (code not shown for sake of realty). Generally, what I am looking to accomplish, is to use JavaScript to loop and parse through each of the two known values (also pulled through PHP/cURL) "dateset1" and "dateset2" input fields and then have the day span difference results show in "age" input field.

Obviously, I can only retrieve the values and show results from the first instance because of not having a unique id or name for each instance. Both values in dateset1 and dateset2 are strict values that comes with both the date and a 12 hour time format exactly as shown. The calculation between dateset1 and dateset2 are for dates only and not the time. The time is being igonored and for other future formatting reasons, hence, the "var pattern".

After all calculations and results have been performed, "statusAlert" is then fired and would loop and parse through each "age" input field. If the value is over 365, the results would show in the "status" field as "Alert". If under 365, the results would show "OK". Issues between the chair and the PC on my end have been countless, but I'm at a loss on how to loop through each of these fields without a unique id. Any help would be greatly appreciated...

<!doctype html>
<html>
<head>
<title>Date Diff | V1.03</title>

<script type="text/javascript">  
    function ageCount() {
        //VARS FOR DATESET 1
        var dateset1 = document.getElementById("dateset1").value;
        var date1 = new Date(dateset1);

        //VARS FOR DATESET 2
        var dateset2 = document.getElementById("dateset2").value;
        var date2 = new Date(dateset2);

        var pattern = /^(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

        // TEST DATE PATTERN 1
        if (pattern.test(dateset1)) {

        } else {

            alert("Invalid dateset 1 format. Input should be (dd/mm/yyyy) format!");
            return false;
        }

        if (pattern.test(dateset2)) {
            var oneday = 1000 * 60 * 60 * 24
            var y1 = date1.getTime();    //GET DATESET 1 NEW DATE
            var y2 = date2.getTime();    //GET DATESET 2 NEW DATE
            var age = Math.abs(y1 - y2); //SUBTRACT DATESET 2 FROM DATESET 1

            document.getElementById("age").value=Math.round(age/oneday);
            return true;

        } else {
            alert("Invalid dateset 2 format. Input should be (dd/mm/yyyy) format!");
            return false;
        }

    }

    function statusAlert()  
        {  
        if (document.form1.age.value>=365)

        document.getElementById("status").value = 'Alert';  
        else  

        document.getElementById("status").value = 'OK';  
    }

window.onload=ageCount;
</script>




<style>          
.tablewidth
    {
    width: 750px;
    }

</style>


</head>
<body>

<form name="form1">
<table class="tablewidth">
    <thead>
        <tr>
            <th>Contact Name</th>
            <th>Email</th>
            <th>Last Login</th>
            <th>Current Date</th>
            <th>Day Span</th>
            <th>Status</th>
        </tr>
    </thead>

    <tbody>
        <tr class="">
            <td>Doe, John</td>
            <td>johnd@email.com</td>
            <td class="center"><input type="text" size="20" name="dateset1" id="dateset1" value="3/26/2013 11:02 PM"/></td>
            <td class="center"><input type="text" size="20" name="dateset2" id="dateset2" value="3/25/2014 1:01 PM"/></td>
            <td class="center"><input type="text" size="11" name="age" id="age" value="  "/></td>
            <td class="center"><input type="text" size="11" name="status" id="status" value="  "/></td>
        </tr>
    </tbody>

    <tbody>
        <tr class="">
            <td>Doe, Jane</td>
            <td>janed@email.com</td>
            <td class="center"><input type="text" size="20" name="dateset1" id="dateset1" value="9/05/2013 12:01 PM"/></td>
            <td class="center"><input type="text" size="20" name="dateset2" id="dateset2" value="3/25/2014 1:01 PM"/></td>
            <td class="center"><input type="text" size="11" name="age" id="age" value="  "/></td>
            <td class="center"><input type="text" size="11" name="status" id="status" value="  "/></td>
        </tr>
    </tbody>

    <tbody>
        <tr class="">
            <td>Doe, Judy</td>
            <td>judyd@email.com</td>
            <td class="center"><input type="text" size="20" name="dateset1" id="dateset1" value="3/04/2013 12:01 PM"/></td>
            <td class="center"><input type="text" size="20" name="dateset2" id="dateset2" value="3/25/2014 1:01 PM"/></td>
            <td class="center"><input type="text" size="11" name="age" id="age" value="  "/></td>
            <td class="center"><input type="text" size="11" name="status" id="status" value="  "/></td>
        </tr>
    </tbody>

    <tbody>
        <tr class="">
            <td>Doe, Joe</td>
            <td>joed@email.com</td>
            <td class="center"><input type="text" size="20" name="dateset1" id="dateset1" value="1/08/2013 12:01 PM"/></td>
            <td class="center"><input type="text" size="20" name="dateset2" id="dateset2" value="3/25/2014 1:01 PM"/></td>
            <td class="center"><input type="text" size="11" name="age" id="age" value="  "/></td>
            <td class="center"><input type="text" size="11" name="status" id="status" value="  "/></td>
        </tr>
    </tbody> 

</table>
<input type="button" value="Status Check" onclick='statusAlert()' />
</form>
</body>
</html>

Edited by VNexus

1
Contributor
1
Reply
8
Views
3 Years
Discussion Span
Last Post by VNexus
0

Hi Everyone,

I have found a fix for the above request by using PHP rather than using Javascript. However, I've re-edited and reposted the above script below as it works great if only needing to look at two different dates. Hopefully, it will be usefull for someone that may need something like it. Thank you to all who have taken a peek!

<!doctype html>
<html>
<head>
<title>Date Diff | V1.04</title>

<!-- CLIENT SIDE JS ========= -->
<script type="text/javascript">
function ageCount() {
        //VARS FOR DATESET 1
        var dateset1 = document.getElementById("dateset1").value;
        var date1 = new Date(dateset1);

        //VARS FOR DATESET 2
        var dateset2 = document.getElementById("dateset2").value;
        var date2 = new Date(dateset2);

        //VAR PATTERN FOR DATE AND TIME TYPES
        var pattern = /^(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

        // TEST DATESET1 PATTERN
        if (pattern.test(dateset1)) {
        } else {
            alert("Invalid dateset 1 format. Input should be (dd/mm/yyyy) format!");
            return false;
        }

        // TEST DATESET2 PATTERN
        if (pattern.test(dateset2)) {

        var oneday = 1000 * 60 * 60 * 24
        var y1 = date1.getTime();       //GET DATESET 1 NEW DATE
        var y2 = date2.getTime();       //GET DATESET 2 NEW DATE
        var age = Math.abs(y1 - y2);    //SUBTRACT DATESET 2 FROM DATESET 1

        document.getElementById("age").value=Math.round(age/oneday);
        return true;
        } else {
            alert("Invalid dateset 2 format. Input should be (dd/mm/yyyy) format!");
            return false;
        }
    }

function statusAlert()
        {
        if (document.form1.age.value>=365)
            document.getElementById("status").value = 'Alert';

        else
            document.getElementById("status").value = 'OK';
        }
window.onload=ageCount;
</script>

<!-- CLIENT SIDE CSS ======== -->
<style>
.tablewidth
        {
        width: 750px;
        }
</style>
</head>

<!-- BEGIN FORM ============= -->
<body>
    <form name="form1">
        <table class="tablewidth">
            <thead>
                <tr>
                    <th>Contact Name</th>
                    <th>Email</th>
                    <th>Last Login</th>
                    <th>Current Date</th>
                    <th>Day Span</th>
                    <th>Status</th>
                </tr>
            </thead>

            <tbody>
                <tr class="">
                    <td>Doe, John</td>
                    <td>johnd@email.com</td>
                    <td><input type="text" size="20" name="dateset1" id="dateset1" value="3/26/2013 11:02 PM"/></td>
                    <td><input type="text" size="20" name="dateset2" id="dateset2" value="3/25/2014 1:01 PM"/></td>
                    <td><input type="text" size="11" name="age" id="age" value=" "/></td>
                    <td><input type="text" size="11" name="status" id="status" value=" "/></td>
                </tr>
            </tbody>
        </table>
        <input type="button" value="Status Check" onclick='statusAlert()' />
    </form>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.