tv4free
tv4free

Reputation: 287

Need clarity on javascript date logic

I am getting false for both conditions

localStorage.getitem("dl-visited-date") // "Mon Oct 07 2013 13:58:18 GMT-0400 (EDT)";
currentDate // Tue Oct 08 2013 14:18:26 GMT-0400 (EDT)
currentDate > localStorage.getItem("dl-visited-date") //false
currentDate < localStorage.getItem("dl-visited-date") //false

Upvotes: 0

Views: 91

Answers (2)

KAUSHAL J. SATHWARA
KAUSHAL J. SATHWARA

Reputation: 994

$(function () {
    var dateformate = localStorage.getItem("selectedFormat");

    alert(dateformate);
}); 

Upvotes: 0

Bergi
Bergi

Reputation: 664484

localStorage.getitem does return a string (your Date object was implicitly stringified when you stored it in the localstorage). If you compare this with a Date object, both will be casted to numbers, but while this works for the Date object the string will become NaN. And that compares false to anything.

You will need to parse it before (using the Date constructor):

var date = new Date(localStorage.getitem("dl-visited-date")),
    currentDate = new Date();

If you want to test them for equality, you will need to use plain numbers instead. Use Date.parse then:

var dateStamp = Date.parse(localStorage.getitem("dl-visited-date")),
    currentDateStamp = Date.now();

Upvotes: 3

Related Questions