Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JavaScript to calculate hours between a start time/date and end time/date

New Here ,
Apr 20, 2023 Apr 20, 2023

I've looked all over in the community forums and tried a few things that people suggested 

 

I am having trouble finding the right script to calculate hours between a start date/time and the end date/time. 

I am not sure if the problem lies with the input fields or the spot where I am inputting the script

 

I never have to work with scripts - just need some help this one time. 

TOPICS
JavaScript
28.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 18, 2023 May 18, 2023

You have wrong fields in 'datestarted' and 'datefinished' variables.

Try this:

 

var timefinished = this.getField("TIME.FINISHED").valueAsString;
var timestarted = this.getField("TIME.STARTED").valueAsString;

var datefinished = this.getField("EndDate").valueAsString;
var datestarted = this.getField("StartDate").valueAsString;

if (datefinished && datestarted && timefinished && timestarted) {
	var datetimefinished = util.scand("yyyy-mm-dd HH:MM", datefinished + " " + timefinished);
	var datetimestarted = util.scand("yyyy-mm-dd HH:MM", datestarted + " " + timestarted);

event.value = (datetimefinished - datetimestarted)/(60 * 60 * 1000);
} else event.value = "";

 

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2023 May 18, 2023

Nesa - awesome thanks - finally a solution!!  Have a wonderful day!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 30, 2024 Oct 30, 2024

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Time Calculator</title>

    <link rel="stylesheet" href="style.css">

</head>

<body>

    <div class="calculator">

        <h1>Time Calculator</h1>

        <div>

            <label>Hours:</label>

            <input type="number" id="hours" value="0">

        </div>

        <div>

            <label>Minutes:</label>

            <input type="number" id="minutes" value="0">

        </div>

        <div>

            <label>Seconds:</label>

            <input type="number" id="seconds" value="0">

        </div>

        <button onclick="calculate()">Calculate Total Time</button>

        <p id="result"></p>

    </div>

    <script src="script.js"></script>

</body>

</html>

body {

    font-family: Arial, sans-serif;

    display: flex;

    justify-content: center;

    align-items: center;

    height: 100vh;

    margin: 0;

    background-color: #f0f0f0;

}

 

.calculator {

    background: #fff;

    padding: 20px;

    border-radius: 8px;

    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

    text-align: center;

}

 

.calculator h1 {

    color: #333;

}

 

.calculator div {

    margin: 10px 0;

}

 

.calculator label {

    margin-right: 10px;

}

 

.calculator input {

    width: 60px;

    padding: 5px;

    font-size: 1rem;

}

 

button {

    padding: 10px 20px;

    font-size: 1rem;

    color: #fff;

    background-color: #007bff;

    border: none;

    border-radius: 4px;

    cursor: pointer;

}

 

button:hover {

    background-color: #0056b3;

}

 

#result {

    margin-top: 20px;

    font-size: 1.2rem;

    color: #333;

}

function calculate() {

    const hours = parseInt(document.getElementById('hours').value) || 0;

    const minutes = parseInt(document.getElementById('minutes').value) || 0;

    const seconds = parseInt(document.getElementById('seconds').value) || 0;

 

    const totalSeconds = (hours * 3600) + (minutes * 60) + seconds;

    const displayHours = Math.floor(totalSeconds / 3600);

    const displayMinutes = Math.floor((totalSeconds % 3600) / 60);

    const displaySeconds = totalSeconds % 60;

 

    document.getElementById('result').innerText =

        `Total Time: ${displayHours} hours, ${displayMinutes} minutes, ${displaySeconds} seconds`;

 

}

For more you can visit Calcularhoras

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 01, 2024 Jun 01, 2024

Calculating the hours between a start date/time and an end date/time can indeed be tricky if you're not familiar with scripting. Here's a simple JavaScript function you can use:

function calculateHours(startDate, endDate) {
const start = new Date(startDate);
const end = new Date(endDate);
const milliseconds = Math.abs(end - start);
const hours = milliseconds / (1000 * 60 * 60);
return hours;
}

// Example usage
const startDateTime = '2024-06-01T08:00:00'; // Format: YYYY-MM-DDTHH:MM:SS
const endDateTime = '2024-06-01T12:30:00'; // Format: YYYY-MM-DDTHH:MM:SS
const hoursDifference = calculateHours(startDateTime, endDateTime);
console.log('Hours between start and end:', hoursDifference);

This function takes the start date/time and end date/time as inputs, converts them to Date objects, calculates the difference in milliseconds, and then converts it to hours. You can use this function by passing your start and end date/time values to it.

If you're seeking more information or resources regarding date and time calculations, feel free to reach out!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 06, 2025 Mar 06, 2025
LATEST



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines