<!DOCTYPE html>

<html>

<head>

  <title>Contraction Script</title>

  <link rel="stylesheet" type="text/css" href="table {

  border-collapse: collapse;

  width: 100%;

}


table, th, td {

  border: 1px solid black;

  padding: 8px;

}


th {

  background-color: #f2f2f2;

}

">

</head>

<body>

  <h1>Contraction Script</h1>

  <form id="contractionForm">

    <label for="startTime">Start Time:</label>

    <input type="text" id="startTime" required>


    <label for="endTime">End Time:</label>

    <input type="text" id="endTime" required>


    <button type="button" onclick="addContraction()">Add Contraction</button>

  </form>


  <table id="contractionTable">

    <tr>

      <th>Start Time</th>

      <th>End Time</th>

      <th>Duration</th>

    </tr>

  </table>


  <script src="function addContraction() {

  var startTime = document.getElementById("startTime").value;

  var endTime = document.getElementById("endTime").value;


  var duration = calculateDuration(startTime, endTime);

  if (duration === null) {

    alert("Invalid time format. Please enter time in HH:MM format.");

    return;

  }


  var table = document.getElementById("contractionTable");

  var row = table.insertRow(-1);


  var startTimeCell = row.insertCell(0);

  var endTimeCell = row.insertCell(1);

  var durationCell = row.insertCell(2);


  startTimeCell.innerHTML = startTime;

  endTimeCell.innerHTML = endTime;

  durationCell.innerHTML = duration;

}


function calculateDuration(startTime, endTime) {

  var start = parseTime(startTime);

  var end = parseTime(endTime);


  if (start === null || end === null) {

    return null;

  }


  var duration = end - start;

  if (duration < 0) {

    duration += 24 * 60; // Add 24 hours if end time is smaller than start time (assuming both times are within the same day)

  }


  return formatTime(duration);

}


function parseTime(time) {

  var parts = time.split(":");

  if (parts.length !== 2) {

    return null;

  }


  var hours = parseInt(parts[0], 10);

  var minutes = parseInt(parts[1], 10);


  if (isNaN(hours) || isNaN(minutes) || hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {

    return null;

  }


  return hours * 60 + minutes;

}


function formatTime(minutes) {

  var hours = Math.floor(minutes / 60);

  var mins = minutes % 60;


  return ("0" + hours).slice(-2) + ":" + ("0" + mins).slice(-2);

}

"></script>

</body>

</html>


Comments

Popular posts from this blog

Rakhra Url shorten App