日付/時刻文字列を指定すると、次のようになります。
2010-12-12 19:27:00Z
JS/jQuery を使用して、それが今日のものかどうかを簡単に判断するにはどうすればよいでしょうか?
ありがとう
日付/時刻文字列を指定すると、次のようになります。
2010-12-12 19:27:00Z
JS/jQuery を使用して、それが今日のものかどうかを簡単に判断するにはどうすればよいでしょうか?
ありがとう
列をオブジェクトDate.parse
文字列をタイムスタンプに変換して、文字new Date
に解析するために使用できます。Date
var date = '2010-12-12 19:27:00Z',
// then = new Date(Date.parse(date)),
then = new Date(date),
// Firefox doesn't support dates in this format
// you can covert it to an accepted format like this:
// then = new Date(date.replace(/(.*) (.*)Z/,'$1T$2'))
now = new Date;
次に、日付を比較できます。
if(then.toDateString() === now.toDateString())
これは、月と日のフィールドが常に 2 桁であることを前提としていますが、このようなものは機能するはずです。
var dateInput = "2010-12-12 19:27:00Z"
var date = dateInput.substring(0, 10); //"2010-12-12"
var d = new Date();
var today = d.getFullYear() + "-" + ( d.getMonth() + 1 ) + "-" + d.getDate();
if( date == today )
match = true;
else
match = false;
var input = new Date('2012-06-07 19:27:00Z');
var curr = new Date();
var iDate = input.getDate+'-'+input.getMonth()+'-'+input.getFullYear();
var currDate = curr.getDate+'-'+curr.getMonth()+'-'+curr.getFullYear();
if(iDate== currDate){
console.log("today");
}else{
console.log("not");
}
デモはこちら