1. 程式人生 > >Js日期處理問題

Js日期處理問題

獲取當前日期

//獲取當前時間,格式YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }

比較日期大小

方法1:

function CompareDate(d1,d2)
{
  return ((new Date(d1.replace(/-/g,"\/"))) > (new Date(d2.replace(/-/g,"\/"))));
}
 
var current_time = "2007-2-2 7:30";
var stop_time = "2007-1-31 8:30";
alert(CompareDate(current_time,stop_time));

方法2:

var curTime = new Date();
//2把字串格式轉換為日期類
var startTime = new Date(Date.parse(kc.begintime));
var endTime = new Date(Date.parse(kc.endtime));
 
//3進行比較
return (curTime>=startTime && curTime<=endTime);