0

i'm trying to calculate between a date i put into localstorage and the current timestamp. I want to check how much minutes there is between the moment it was saved in localstorage and now. I cant figure out how to save the date in the localstorage as a propper date object tho!

The code is the following:

<html>
<head>
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
</head> 

<input type="text" id="text"> 
<button value="save" onClick="setStorage()" id="btn"> Save </button>



<script>
function setStorage(){
    var currentTime = new Date();
    var text = $("#text").val();
    var testobj = {'test' : 1, 'refresh' : currentTime};
    localStorage.setItem('storage', JSON.stringify(testobj));
}
// setinterval code is on different page
        setInterval(function() {
        var date2 = new Date();
        var retreived = localStorage.getItem('storage');
        var date1 = new Date(retreived['refresh'])
        console.log(date1);
        var parse = JSON.parse(retreived);
        console.log(date2 - date1);
        $(".label").text(localStorage.getItem('storage'));
     }, 1000);
</script>

The setinterval is on a different page. The outcome of the label is this:

{"test":1,"refresh":"2018-05-24T17:36:06.471Z"}

How could i compare that date with the current date, and check how much minutes there is between those 2 times?

Thanks!

  • 1
    `I cant figure out how to save the date in the localstorage as a propper date object tho!` Don't - instead, save the timestamp only, and parse it into a Date / human readable text only when needed, you'll save yourself a ton of confusion – CertainPerformance May 24 '18 at 21:49
  • Create a new data from the stored string and then compare the two. https://stackoverflow.com/questions/8224459/how-to-create-a-date-object-from-string-in-javascript – Scott Marcus May 24 '18 at 21:49
  • You could go with both approaches, but I'd prefer @CertainPerformance's solution. Timestamp's are more flexible. – doubleOrt May 24 '18 at 21:53
  • Related to your question: https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage – doubleOrt May 24 '18 at 22:01
  • Possible duplicate of [Issues with Date() when using JSON.stringify() and JSON.parse()](https://stackoverflow.com/questions/11491938/issues-with-date-when-using-json-stringify-and-json-parse) – doubleOrt May 24 '18 at 22:03

2 Answers2

0

Just change var date1 = new Date(retreived['refresh']) with

var date1 = new Date(JSON.parse(retreived)['refresh'])

and

console.log(date2.getMinutes() - date1.getMinutes());
Gwen
  • 444
  • 2
  • 10
  • The difference in minutes should be more like `(date2 - date1) / 6e4`, see [*Get the Difference Between Two Dates in Minutes*](https://stackoverflow.com/questions/33638494/get-the-difference-between-two-dates-in-minutes) – RobG May 25 '18 at 06:25
0

I cant figure out how to save the date in the localstorage as a propper date object tho!

  • Don't - instead, save the timestamp only, and parse it into a Date / human readable text only when needed, you'll save yourself a ton of confusion

This worked for me! thanks @CertainPerformance