【问题标题】:Get System time, localstorage, compare dates and performe an action获取系统时间、本地存储、比较日期并执行操作
【发布时间】:2021-02-05 17:36:56
【问题描述】:

我需要获取一次系统时间,然后将其存储在 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"));

【问题讨论】:

  • 对不起,我忘了说我正在使用moment.js进行日期比较。
  • 目前您正在尝试将字符串作为日期进行比较,为什么?存储的日期不会总是早于当前日期吗?
  • 是的,它会更旧。我需要说的是,您今天运行该页面,以便存储该日期。每次运行该页面时,脚本都应该检查存储的日期,并且当过去了 15 天时,脚本应该将您重定向到另一个页面(这最后一部分不包含在我发布的代码中) .问题是我需要一个代码修复,以便日期只存储一次,第一次运行。我在那里堆。

标签: javascript date comparison storage


【解决方案1】:

下面的函数将检查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');

【讨论】:

  • 通过分析您简单而整洁的解决方案,我可以说这正是我的意思。但我会等到明天来检查它是否真的有效,因为我只设置了 1 天的差异来测试它。我设置了 0 天并且它重定向了,所以我认为它会起作用。
  • 嗯,正如我所说,一天过去了,它工作得很好。现在我要设置一个延迟,因为重定向会立即发生,我想让用户仍然看到 index.page。但它确实有效。
  • 对不起 Emiel Zuurbier 但我匆忙接受了你的回答,但没有投赞成票。我刚才做了。希望这足以弥补我记忆力差的问题。
【解决方案2】:

现在最终的代码是这样的

    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);

【讨论】:

    猜你喜欢
    • 2014-09-08
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2015-12-05
    相关资源
    最近更新 更多