JS:
$(function() {
var jobDateText = $(".job-date").html()
, jobDate = new Date(jobDateText)
, today = new Date()
, thirtyDaysInMS = 30 * 24 * 60 * 60 * 1000
, differenceInMS = Math.abs(today - jobDate)
, differenceInDays = Math.floor(differenceInMS / 1000 / 60 / 60 / 24);
if (differenceInMS > thirtyDaysInMS) $(".message").show();
$("span.daysOld").html(differenceInDays);
});
HTML:
<span class="job-date">1-20-2013</span>
<div class="message">This job is <span class="daysOld"></span> days old, and it may no longer be available</div>
CSS:
.message {
display:none;
}
对于这样的事情,AngularJS 是 jQuery 的一个非常好的替代品。您在 AngularJS 中所要做的就是在控制器范围内设置一个变量,称为类似$scope.stale = true
并$scope.daysOld
基于日期数学,然后在您的 html 中简单地
<div class="message" ng-show="stale">This job is {{daysOld}} days old it may no longer be available</div>