# How to find the difference between two TIME PICKER?

**URL:** <https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [April 5, 2020, 4:50pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302 "2020-04-05T16:50:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 5, 2020, 4:50pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/1 "2020-04-05T16:50:55Z")

</div>

I have two time pickers: $w(‘#loginTP’) and $w(‘#logoutTP’)  
How do I get the difference between these two and set it on an input bar named $w(‘#workedHours’)?

Thank you.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 5, 2020, 5:32pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/2 "2020-04-05T17:32:49Z")

</div>

Hi @freedfoxworldwide , what do you mean by the difference?

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 5, 2020, 5:37pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/3 "2020-04-05T17:37:14Z")

</div>

@ahmadnasriya For example, the logout time is 5:00 pm and the login time is 1:00 pm. I would like to get their difference so 5:00 - 1:00 is 4 hours. How to implement that? 🙂 I tried searching but found nothing here yet

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 5, 2020, 5:50pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/4 "2020-04-05T17:50:34Z")

</div>

@freedfoxworldwide I don’t think there’s an API to calculate the difference, but I got a comment on your case, you apparently want to calculate the difference between the login and logout to determine how much an employee worked, but being logged in doesn’t mean they’re working, you can login and then sleep.

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 5, 2020, 5:53pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/5 "2020-04-05T17:53:19Z")

</div>

@ahmadnasriya Yup. This is on our dashboard, not the employee. 🙂 Thanks for your concern. We can actually put the difference manually but we’d love to automate it. I hope there’s someone who can help. The date picker can actually have the difderence. I found an example earlier, but we are using the date picker though.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 5, 2020, 6:17pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/6 "2020-04-05T18:17:29Z")

</div>

@freedfoxworldwide I have an idea for you, why not using a variable start counting from 0 once the user is logged into the site, and increase by 1 each second, and stops when the user logged out, either by the logout event or by clicking on a button that logs you out, then convert the seconds into hours, minutes and seconds and save it into a database along side the time of logging in and out.

I think this might work for your specific purpose.

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 5, 2020, 6:20pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/7 "2020-04-05T18:20:57Z")

</div>

Wow! I didn’t think of that and it’s wonderful. I’ll try it if I can. We are with the colleague so we can supervise them. We were thinking of getting a time tracking tool to do the work but this is a brilliant idea.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 5, 2020, 8:57pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/8 "2020-04-05T20:57:20Z")

</div>

@freedfoxworldwide Thanks for the compliment 😊 I’ll be glad to assist you code this functionality.

---

<div class="post-metadata">

**Author:** ![givemeawhisky](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/givemeawhisky/32/5383_2.png) [@givemeawhisky](https://forum.wixstudio.com/u/givemeawhisky)\
**Post date:** [April 6, 2020, 12:40am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/9 "2020-04-06T00:40:20Z")

</div>

There are previous posts that will help you with code examples.  
[https://www.wix.com/corvid/forum/community-discussion/find-the-difference-in-hours-between-2-dates](https://www.wix.com/corvid/forum/community-discussion/find-the-difference-in-hours-between-2-dates)  
[https://www.wix.com/corvid/forum/community-discussion/database-time-ago-function](https://www.wix.com/corvid/forum/community-discussion/database-time-ago-function)

Also, Vorbly does it for date pickers here, however you can easily just change it for time pickers instead.  
[https://www.vorbly.com/Vorbly-Code/WIX-DAYS-BETWEEN-TWO-DATEPICKERS](https://www.vorbly.com/Vorbly-Code/WIX-DAYS-BETWEEN-TWO-DATEPICKERS)

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 5:18am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/10 "2020-04-06T05:18:24Z")

</div>

Yes! I tried searching before posting but can’t get it to work. Maybe, I’ll try again. Thank you @givemeawhisky

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 9:58am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/11 "2020-04-06T09:58:11Z")

</div>

@ahmadnasriya I can’t get it to work. I got your idea with a different approach. I am just unsure of my code anymore as some fields aren’t saving. 🤣😥😭

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 6, 2020, 12:53pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/12 "2020-04-06T12:53:23Z")

</div>

Hi, @freedfoxworldwide , use something like this …

```javascript
import wixUsers from 'wix-users';
import wixData from 'wix-data';

let user = wixUsers.currentUser
let counter = 0;
let sessionId
let session

$w.onReady(() => {
    startSession();
})
async function startSession() {
    if (user.loggedIn === true && user.role === "Admin") {
        if (sessionId === null || sessionId === undefined) {
            
            sessionId = Math.floor(((Math.random() * 10000000) * (Math.random() * 367824) * (Math.random() * 4831497))).toString();
           
             let newSessionData = {
                sessionId: sessionId,
                userId: user.id
            }

            await wixData.insert("sessions", newSessionData).then(() => {
                setInterval(async() => {

                    session = newSessionData + { duration: counter++ };
                    await wixData.update("sessions", session)
                }, 1000);
            })
        }
    }
}

```

I hope that helped!  
Ahamd

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 2:31pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/13 "2020-04-06T14:31:17Z")

</div>

@ahmadnasriya I am exploring this now. I would used in onDblClick of a button instead. 😀 I’ll try if I can make it work.

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 4:35pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/14 "2020-04-06T16:35:02Z")

</div>

```javascript
import wixUsers from 'wix-users';
import wixData from 'wix-data';
let user = wixUsers.currentUser
let counter = 0;
let sessionId;
let session;
$w.onReady(function () {

});
///////
export async function login_dblClick(event) {
//Add your code for this event here:
if (sessionId === null || sessionId === undefined) {
sessionId = Math.floor(((Math.random() * 10000000) * (Math.random() * 367824) * (Math.random() * 4831497))).toString();
}
let logIn = new Date();
$w('#datePicker1').value = logIn;
$w('#login').disable();
$w('#logout').enable();
var newSessionData = {
logIn: $w('#datePicker1').value,
sessionId: sessionId,
userId: user.id,
name: user.id,
}
await wixData.insert("timeSheet", newSessionData).then(() => {
setInterval(async () => {
await wixData.update("timeSheet")
}, 1000);
})
}
export async function logout_dblClick(event) {
let logOut = new Date();
$w('#datePicker2').value = logOut;
$w('#logout').disable();
$w('#login').enable();
var newSessionData = {
logOut: $w('#datePicker2').value,
sessionId: sessionId,
userId: user.id,
name: user.id
}
await wixData.insert("timeSheet", newSessionData).then(() => {
setInterval(async () => {
await wixData.update("timeSheet")
}, 1000);
})
}

```

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 5:22pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/15 "2020-04-06T17:22:40Z")

</div>

@ahmadnasriya You can try it here:  
[https://thefaq.wixsite.com/mysite-2/timetest](https://thefaq.wixsite.com/mysite-2/timetest)

Login email: test@freelancer.com  
pw: ahmad

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 6, 2020, 6:41pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/16 "2020-04-06T18:41:22Z")

</div>

@ahmadnasriya As you can see that it submits the login when we double click the login button, but how do we update the submitted value when we click the logout button?  
I have two fields in the database, logIn and logOut  
when the loginbutton is double clicked, the submitted info does not contain the logout of course, onlie the lofIn field is filled. What I want to do is when they click the logoutbutton, the submitted info will be updated so there will be a logOut field value. I guess I should get the \_id of the submitted info and have it on the logoutButton function too so that it wil be used in wixData.update but how do I do that? I can’t seem to get it to work. Thank you for your big help.

@givemeawhisky : I was wondering if you could give me a little help. Thank you. 😀

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 7, 2020, 4:38am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/17 "2020-04-07T04:38:47Z")

</div>

Hi @freedfoxworldwide , sorry for the late reply.

You have the **sessionId** between your hands, you need to query it from the database, and then update it, here’s how.

```javascript
wixData.query("timeSheet").eq("sessionId", sessionId).find()
.then(async (result) => {
    let session = result.items[0];
    session.logout = new Date();
    
    await wixData.update("timeSheet", session);
})

```

NOTE: If you want to use my idea, the entire code needs to be in the “Site” section, so the counter will not stop or reset when visiting other pages. This called a session, it’ll reset if you closed the tab, stops if your connection was lost.

Hope that helped.  
Ahmad

---

<div class="post-metadata">

**Author:** ![freedfoxworldwide](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/freedfoxworldwide/32/876_2.png) [@freedfoxworldwide](https://forum.wixstudio.com/u/freedfoxworldwide)\
**Post date:** [April 7, 2020, 9:56am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/18 "2020-04-07T09:56:09Z")

</div>

@ahmadnasriya It greatly help! It’s working now. Thank you. \<3

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 7, 2020, 11:22am UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/19 "2020-04-07T11:22:48Z")

</div>

I’m happy to hear that it’s working.  
You’re welcome friend.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [April 7, 2020, 9:35pm UTC](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302/20 "2020-04-07T21:35:05Z")

</div>

@freedfoxworldwide Don’t forget to mark this question as answered so they can close it.

[Next page](https://forum.wixstudio.com/t/how-to-find-the-difference-between-two-time-picker/28302.md?page=2)
