开发者

Javascript, typecasting needed (I think)

I am reading a date from local storage and then need to compare it to another date but it does not work. I think I need to typecast it but am very rusty with Javascript, please have a look, my code is not much:

// ****** ### IMPORTANT: The below line returns "2011,3,20" ### *******
var da_expiry_dat开发者_如何学编程e = localStorage['list_expiry_date']; 

var today_date = new Date();
var future_date = new Date();

future_date.setFullYear(da_expiry_date+2);
alert (future_date+"\n"+today_date);

    if (future_date>today_date) {
        alert("1");
    }
    else {
        alert("2");
    }

Thanks in advance!


If you're comment at the top is correct and da_expiry_date is returning a string, then you will need to typecast it to date before you can do anything meaningful. The easiest way to do this is with oneof the methods listed here. The problem with converting a string to a date is knowing the format; if you have all the pieces separated you can just use one of the other date constructors. :D

As it is, you code is trying to take the string "2011,3,20", add the number 2 to it, and set it as the full year of future_date. The browser ends up converting 2 to a string and concatenating that to the end, giving you "2011,3,202". setFullYear expects an int instead of a string, so it can't do its job.

You probably want that line switched to this after you've got your date properly converted:

future_date.setFullYear(da_expiry_date.getFullYear()+2);


I'm not sure what you're attempting with this line:

da_expiry_date+2;

The result of that is:

2011,3,202

...which is not a valid value to pass to setFullYear. So, let's parse the date:

var da_expiry_date = localStorage['list_expiry_date']; 

function pad(num, n) {
    return ("0" + num).slice(-n);
}

function formatDate(date) {
    var p = date.split(",");
    return [pad(p[0], 4), pad(p[1], 2), pad(p[2], 2)].join("-");
}

var today_date = new Date();
var future_date = new Date(formatDate(da_expiry_date));

alert (future_date+"\n"+today_date);

if (future_date>today_date) {
    alert("1");
} else {
    alert("2");
}

If you're attempting to add 2 to the year, then do it after you've split the date into its components:

function formatDate(date) {
    var p = date.split(",");
    p[0] = p[0] * 1 + 2; // add 2 to the year
    return [pad(p[0], 4), pad(p[1], 2), pad(p[2], 2)].join("-");
}

Or, even better, parameterize it:

function formatDate(date, n) {
    var p = date.split(",");
    p[0] = p[0] * 1 + n; // add n to the year
    return [pad(p[0], 4), pad(p[1], 2), pad(p[2], 2)].join("-");
}


You'll need to parse it as a date first, try this:

var dateFromLocalStorage = function(s) {
  var m = (""+s).match(/^(\d+),(\d+),(\d+)$/);
  return (m) ? new Date(m[1], m[2], m[3]) : null;
};
dateFromLocalStorage("2011,3,20"); // => Wed Apr 20, 2011 ...

Note that the date constructor takes the month as a zero based integer, so Jan=0, Feb=1, Mar=2, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜