jimingminlovefly

统计

最新评论

js-日期计算

1.计算当前日期后的N天
function date2str(n){
 var s, d, t, t2;
 t = new Date().getTime();
    t2 = n * 1000 * 3600 * 24;
    t+= t2;
    d = new Date(t);
    s = d.getUTCFullYear() + "-";
    s += ("00"+(d.getUTCMonth()+1)).slice(-2) + "-";
    s += ("00"+d.getUTCDate()).slice(-2);
    return s;
}

2.计算给定日期后的N天
function str2date(str,n){  
  var   dd, mm, yy;  
  var   reg = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
  if (arr = str.match(reg)) {
    yy = Number(arr[1]);
    mm = Number(arr[2])-1;
    dd = Number(arr[3]);
  } else {
    var d = new Date();
    yy = d.getUTCFullYear();
    mm = ("00"+(d.getUTCMonth())).slice(-2);
    dd = ("00"+d.getUTCDate()).slice(-2);
  }
  return date2str(yy, mm, dd,n);
}

3.计算给定日期后的N天
function date2str(yy, mm, dd,n) {
    var s, d, t, t2;
    t = Date.UTC(yy, mm, dd);
    t2 = n * 1000 * 3600 * 24;
    t+= t2;
    d = new Date(t);
    s = d.getUTCFullYear() + "-";
    s += ("00"+(d.getUTCMonth()+1)).slice(-2) + "-";
    s += ("00"+d.getUTCDate()).slice(-2);
    return s;
}

posted on 2011-11-08 11:29 计明敏 阅读(2972) 评论(0)  编辑  收藏 所属分类: js


只有注册用户登录后才能发表评论。


网站导航: