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

标签 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...
    }
});

最佳答案

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 ' );
}

关于javascript - 如何使用日期字符串并检查它是否是今天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28118098/

相关文章:

html - css 框模型列高度重叠

javascript - 如何从另一个 javascript 调用 javascript 函数,其中两者都在同一个 html 上

html - 在最后一行调整 flex 元素的大小

javascript - 传递查询字符串后自动在不同页面提交表单?

javascript - 关闭浏览器后保存变量

javascript - window.on ('load' , function() {}) 完成后页面元素仍在加载?

javascript - 获取 Uncaught ReferenceError : function is not defined

javascript - 在谷歌浏览器的检查元素中显示的 == $0 是什么意思对于所选元素

javascript - 获取所有接触矩形的元素

javascript - 访问由 CSS 类设置的 HTML 属性