我如何使用一个日期字符串并检查它是否是今天

时间:2015-01-23 20:20:24

标签: javascript html

如何使用日期字符串并检查它是否为今天

我有以下隐藏字段:

<input type="hidden" id="datThe" value="01192015" />

如何查看今天是否01192015

更新(不工作):

<input type="hidden" value="closeHuguenot" id="statHuguenot" />
<input type="hidden" value="01232015" id="dateHuguenot" />

$(function () {
    var date = new Date(); // Initiate a new date object
    var day = date.getDate(); // Gets the current day
    day = (day.length < 2) ? day : '0' + day; // This will add 0 before the date if its less than 10
    var month = date.getMonth() + 1; // Because js months starts at 0
    month = (month.length < 2) ? month : '0' + month; // Will add 0 before the month if less than 10
    var year = date.getFullYear(); // Gets the current year
    var formatted_date = month + day + year; // Puts them together as your date is formatted

    var sHuguenot = $("#statHuguenot").val();
    var dHuguenot = $("#dateHuguenot").val();
    if (sHuguenot.toLowerCase() == "closehuguenot" && dHuguenot == formatted_date.toString()) {
        $("#Label171").html("CLOSED"); //not displaying closed...
    }
});

1 个答案:

答案 0 :(得分:2)

var date = new Date(); // Initiate a new date object
var day = date.getDate(); // Gets the current day
day = ( day > 10 ) ? day : '0' + day; // This will add 0 before the date if its less than 10
var month = date.getMonth()+1; // Because js months starts at 0
month = ( month > 10 ) ? month : '0' + month; // Will add 0 before the month if less than 10
var year = date.getFullYear(); // Gets the current year
var formatted_date = month + day + year; // Puts them together as your date is formatted

// Check if the current date is equal to your input value
if( document.getElementById('datThe').value == formatted_date.toString() ){
    alert( ' The date is today ' );
}