2017-02-10 83 views
0

我在JS以下功能:的Javascript比較日期總是假

var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/'); 
var endDate = stringToDate('01/01/2017','mm/dd/yyyy','/'); 


function compareDate(_date) { 

    var val1 = (startDate <= _date) 
    var val2 = (endDate >= _date) 
    var val = val1 && val2 

    log('--------') 
    log(_date) 
    log(startDate) 
    log(endDate) 
    log(val1) 
    log(val2) 
    log(val) 
    log('--------') 
    return val 
} 

日誌

[17-02-10 13:25:14:145 EET]週五2016年12月2日00 :00:00 GMT + 0200(EET)

[17-02-10 13:25:14:146 EET]週四12月01 00:00:00 GMT + 02:00 2016

[17- 02-10 13:25:14:147 EET] Sun Jan 01 00:00:00 GMT + 02:00 2 017

[17-02-10 13:25:14:147 EET]假

[17-02-10 13:25:14:148 EET]假

[17-02- 10 13:25:14:148 EET]假

[17-02-10 13:25:14:149 EET] --------

[17-02-10 13: 25:14:149 EET] --------

[17-02-10 13:25:14:150 EET] Sat Dec 03 2016 00:00:00 GMT + 0200(EET)

[17-02-10 13:25:14:150 EET]週四12月01 00:00:00 GMT + 02:00 2016

[17-02-10 13:25:14:151 EET]孫1月1日00:00:00 GMT + 02:00 2017

[17-02-10 13:25:14:151 EET]假

[17-02-10 13時25: 14:152 EET]假

[17-02-10 13:25:14:152 EET]假

[17-02-10 13:25:14:152 EET] - - - - ---

[17-02-10 13:25:14:153 EET] --------

[17-02-10 13:25:14:153 EET]太陽2016年12月4日00:00:00 GMT + 0200(EET)

[17-02-10 13:25:14:154 EET]週四12月01 00:00:00 GMT + 02:00 2016

[17 -02-10 13:25:14:154 EET]太陽1月1日00:00:00 GMT + 02:00 2017

[17-02-10 13:25:14:155 EET]假

[17-02-10 13:25:14:155 EET ]假

[17-02-10 13:25:14:156 EET]假

正如你可以在第一個日誌Dec 02看到的是大於Dec 01但我越來越假等等...

+1

請嘗試谷歌。 http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript –

+0

什麼類型是* _date *?我猜它是一個字符串,所以你要比較一個字符串基元和Date對象。請參閱[*如何創建最小,完整和可驗證的示例*](http://stackoverflow.com/help/mcve)。 – RobG

+0

[用JavaScript比較兩個日期]可能的重複(http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – evolutionxbox

回答

0

一些代碼是從你的問題失蹤,但我說我對它的版本 - >更改字符串日期整數與格式yyyymmdd>= & <=比較整數(如字符串比較可能是錯誤的):

function stringToDateInt(_date, _format, _sep) 
 
{ 
 
    var aDate = _date .split(_sep); 
 
    var aForm = _format.split(_sep); 
 
    //console.log(aDate); 
 
    //console.log(aForm); 
 
    var oDate = {}; 
 
    for(var i = 0, length = aDate.length; i < length; i++) 
 
    { 
 
    oDate[aForm[i]] = aDate[i]; 
 
    } 
 
    //console.log(oDate); 
 
    return (oDate.yyyy + oDate.mm + oDate.dd) * 1; 
 
} 
 

 
var startDate = stringToDateInt('12/01/2016','mm/dd/yyyy','/'); 
 
var endDate = stringToDateInt('01/01/2017','mm/dd/yyyy','/'); 
 

 
function compareDate(_date) 
 
{ 
 
    var iDate = stringToDateInt(_date,'mm/dd/yyyy','/'); 
 
    var val1 = (startDate <= iDate); 
 
    var val2 = (endDate >= iDate); 
 
    var val = val1 && val2; 
 

 
    console.log('--------') 
 
    console.log(_date, '->', iDate); 
 
    console.log(startDate) 
 
    console.log(endDate) 
 
    console.log('Date Bigger than or equal startDate:', val1) 
 
    console.log('Date Less than or equal endDate:', val2) 
 
    console.log('Date between start and end dates:', val) 
 
    console.log('--------') 
 
    return val 
 
} 
 

 
compareDate('12/02/2016'); 
 

 
compareDate('01/01/2015'); 
 

 
compareDate('11/11/2017');

或者最新版本更接近你在做什麼:

function stringToDate(_date, _format, _sep) 
 
{ 
 
    var aDate = _date .split(_sep); 
 
    var aForm = _format.split(_sep); 
 
    //console.log(aDate); 
 
    //console.log(aForm); 
 
    var oDate = {}; 
 
    for(var i = 0, length = aDate.length; i < length; i++) 
 
    { 
 
    oDate[aForm[i]] = aDate[i]; 
 
    } 
 
    //console.log(oDate); 
 
    //return (oDate.yyyy + oDate.mm + oDate.dd) * 1; 
 
    return new Date(oDate.yyyy, (oDate.mm * 1 - 1), oDate.dd); 
 
} 
 

 
var startDate = stringToDate('12/01/2016','mm/dd/yyyy','/'); 
 
var endDate = stringToDate('01/01/2017','mm/dd/yyyy','/'); 
 

 
function compareDate(_date) 
 
{ 
 
    var val1 = (startDate <= _date); 
 
    var val2 = (endDate >= _date); 
 
    var val = val1 && val2; 
 

 
    console.log('--------') 
 
    console.log(_date, '->', _date); 
 
    console.log(startDate) 
 
    console.log(endDate) 
 
    console.log('Date Bigger than or equal startDate:', val1) 
 
    console.log('Date Less than or equal endDate:', val2) 
 
    console.log('Date between start and end dates:', val) 
 
    console.log('--------') 
 
    return val 
 
} 
 

 
compareDate(stringToDate('12/02/2016','mm/dd/yyyy','/')); 
 

 
compareDate(stringToDate('01/01/2015','mm/dd/yyyy','/')); 
 

 
compareDate(stringToDate('11/11/2017','mm/dd/yyyy','/'));

+0

你已經做出了一些大膽的假設,但是OP沒有透露如何調用該函數或者* stringToDate *做了什麼。無論是字符串還是數字,比較'yyyymmdd'都可以。順便說一句,在* stringToDate *中,'oDate.mm'應該是'oDate.mm - 1'。 ;-) – RobG

+0

好點 - 會相應地編輯它 –