How can I get the current time in Wix?

Not the date, the time. I want to automatically log users out if they’ve been logged in for more than 15 minutes.

I see that Wix exposes the Date object, tried calling ‘getTime’ on it, but it tells me it’s not a function. Is there another way to do it?
https://support.wix.com/en/article/corvid-tutorial-displaying-todays-date-with-code

1 Like

https://www.wix.com/corvid/forum/community-discussion/display-current-date-and-time
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleTimeString

If you want to keep a track of the number of seconds the user has been online for. Record the timestamp in UNIX when the user first logs in and then check for completion of 15 minutes (900 seconds) from that timestamp.

Put this on your Site Code tab.

import {session} from 'wix-storage';
import wixUsers from 'wix-users';

$w.onReady(function () {
    setNow(); //set the initial time when the user lands on the page
    setInterval( () => {
        decisions(); //run a check every 3 seconds for the 15 minutes expiry
    }, 3000);
});

function setNow() {
    var d = Date.now();
    var unix = Number(d) / 1000;
    var time = Math.round(unix); //get the current time in seconds
    let value = session.getItem("time");
    if(!value && wixUsers.currentUser.loggedIn === true) {
        session.setItem("time", time); //set the new timestamp only if one does not exist
    }
}
function decisions() {
    var d = Date.now();
    var unix = Number(d) / 1000;
    var time = Math.round(unix); //current timestamp
    let value = session.getItem("time");
    if(Number(value) + 900 > time) { //900 seconds is 15 minutes
      //check if user is logged in and then logout
    }
}