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.
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.
Hi @freedfoxworldwide , what do you mean by the difference?
@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
@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.
@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.
@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.
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.
@freedfoxworldwide Thanks for the compliment I’ll be glad to assist you code this functionality.
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/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
Yes! I tried searching before posting but can’t get it to work. Maybe, I’ll try again. Thank you @givemeawhisky
@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.
Hi, @freedfoxworldwide , use something like this …
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
@ahmadnasriya I am exploring this now. I would used in onDblClick of a button instead. I’ll try if I can make it work.
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);
})
}
@ahmadnasriya You can try it here:
https://thefaq.wixsite.com/mysite-2/timetest
Login email: test@freelancer.com
pw: ahmad
@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.
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.
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
@ahmadnasriya It greatly help! It’s working now. Thank you. <3
I’m happy to hear that it’s working.
You’re welcome friend.
@freedfoxworldwide Don’t forget to mark this question as answered so they can close it.