获取系统时间、本地存储、比较日期并执行操作

Get System time, localstorage, compare dates and performe an action

我需要获取一次系统时间,然后将其存储在 localStorage 中,然后将此存储值与另一个系统日期进行比较,然后在未来日期等于或大于存储的日期时执行操作。我已经尝试过,但我一直坚持让第一次获取系统时间的功能只达到 运行 一次,这样我就可以得到一个未来的日期来进行比较。 这是我的代码

console.log(formatTime());

function formatTime() {
  var date = new Date();
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var seconds = date.getSeconds();
  var ampm = hours >= 12 ? "PM" : "AM";
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? "0" + minutes : minutes;
  return (strTime = date.getDay() + "/" + date.getMonth() + "/" + date.getFullYear() + " " + hours + ":" + minutes + ":" + seconds + " " + ampm);
}

document.getElementById("currentdt").innerHTML = strTime;

var strTime1 = formatTime();
var timeString = JSON.stringify(strTime1);
localStorage.setItem("strTime1", timeString);
var timeStringFromLocalStorage = localStorage.getItem("strTime1");
var timeFromLocalStorage = JSON.parse(timeStringFromLocalStorage);

document.getElementById("time").innerHTML = strTime1;
console.log(timeStringFromLocalStorage);

function compare(dateTimeA, dateTimeB) {
  var momentA = moment(dateTimeA, "strTime1");
  var momentB = moment(dateTimeB, "strTime");
  if (momentA > momentB) return 1;
  else if (momentA < momentB) return -1;
  else return 0;
}

alert(compare("strTime1", "strTime"));

下面的函数将检查localStorage 中是否设置了值。如果没有设置值,它将首先设置它并停止该功能。

如果有一个值,那么它将变成一个moment实例并与当前日期进行比较。如果天数差异等于或大于指定值,它将重定向页面。

function redirectWhenOlderThan(days, url) {
  const storedValue = localStorage.getItem('first-visit');
  const now = moment();
  
  /**
   * If nothing is stored yet, then storedValue will be null.
   * Here you will set the first localStorage item for the first time.
   * Instead of a full date, store the timestamp.
   * Then stop the function.
   */
  if (storedValue === null) {
    localStorage.setItem('first-visit', now.valueOf().toString());
    return;
  }

  /**
   * If there is a stored value then it will be a timestamp as a string.
   * First parse it into a number before putting it into moment.
   * Then check the difference in days between the dates.
   */
  const then = moment(Number(storedValue));
  const difference = now.diff(then, 'days');

  /**
   * If the difference is higher or equal to the given days, redirect.
   */
  if (difference >= days) {
    location.href = url;
  }
}

使用自第一次访问以来应该经过的天数调用该函数,URL 重定向到。

redirectWhenOlderThan(15, 'https://example.com');

我希望这就是你的意思。

旁注: 如果有时间,请深入研究 moment.js。它有很多功能可以节省你一些时间,比如你的 formatTime() 函数,它可以用 moment 写在一行中。

moment().format('DD/MM/YYYY h:mm:ss A');

现在最终的代码是这样的

    function formatTime() {
    var date = new Date();
    return strTime = date.getDay() + '/' + date.getMonth()+'/'+date.getFullYear();

    }
    document.getElementById("currentdt").innerHTML = strTime;


    function redirectWhenOlderThan(days, url) {
    const storedValue = localStorage.getItem('first-visit');
    const now = moment();

    if (storedValue === null) {
    localStorage.setItem('first-visit', now.valueOf().toString());
    return;
    }

    const then = moment(Number(storedValue));
    const difference = now.diff(then, 'days');


    if (difference >= days) {
    location.href = url;
    }
    else {
    window.location.href= 'app/phr.html';
    }
    }
    setTimeout(function () {
    redirectWhenOlderThan(1, 'app/licences.html');
    }, 3000);