【发布时间】:2016-02-10 20:08:04
【问题描述】:
我需要用 javascript 比较日期,所以我写了一个函数:
function fulljson (){
var db_data;
$.ajax({
url: "http://localhost:8888/auction/offers/5",
type: "GET",
async: true,
dataType: "html",
success: function(data) {
var db_data = $.parseJSON(data);
console.log(db_data);
// declare variables
var period_start = new Date('2016-02-24'),
period_now = new Date();
period_end = new Date('2016-11-01'),
//current_date = period_start,
array_of_all_dates = [];
console.log(period_start);
var dodaj = parseInt('3');
period_now = period_now.setDate(period_now.getDate() + dodaj);
console.log(new Date(period_start).getTime() + ' i ' + period_now);
if (new Date(period_start).getTime() > period_now){
current_date = new Date(period_start);
} else {
current_date = new Date(period_now);
current_date.setHours(1
,0,0,0);
}
//current_date = moment(current_date).format('YYYY-MM-DD');
console.log(current_date);
// Create a populated array of dates
// Create a populated array of dates
while (current_date.getTime() <= period_end.getTime()) {
array_of_all_dates.push(current_date);
current_date = new Date(+current_date);
current_date.setDate(current_date.getDate() + 1);
}
// Now loop over the array of populated dates and mutate, so something like
array_of_all_dates = array_of_all_dates.map(function (date) {
var found_in_db = db_data.filter(function (db_data) {
return new Date(db_data.start).getTime() === date.getTime(); // You need to do this comparison better!
});
if (found_in_db.length > 0) {
return found_in_db[0];
}
var new_object = {
title: '',
start: date,
price: '60'
};
console.log(new_object);
return new_object;
});
console.log('result'+array_of_all_dates);
drawCalendar(array_of_all_dates);
},
error: function (data) {
console.log(data);
console.log('GRESKA NEKA');
}
});
//end OF AJAX
};
我的 ajax 函数得到这个数据:
[{"id":82,"price":61,"start":"Mon, 28 Mar 2016 00:00:00 +0000"},{"id":81,"price":61.5,"start":"Sun, 27 Mar 2016 00:00:00 +0000"},{"id":79,"price":61,"start":"Sun, 13 Mar 2016 00:00:00 +0000"},{"id":72,"price":61,"start":"Tue, 29 Mar 2016 00:00:00 +0000"},{"id":66,"price":61,"start":"Sat, 12 Mar 2016 00:00:00 +0000"},{"id":64,"price":60.5,"start":"Fri, 11 Mar 2016 00:00:00 +0000"}]
现在我需要比较日期的函数仅在这种情况下有效,即 2016 年 3 月 27 日。
还有我的period_start is 24. Feb. 和period_end is 1.Nov.2016。
为什么我的函数只在 27.March 有效?
【问题讨论】:
-
这是一些 PHP 模板系统吗?
new Date('{{ date('Y-m-d', strtotime($article->from)) }}')? JS没有strtotime()或date()函数 -
是的,它的 Blade - Laravel...
-
显示实际生成的JS,然后。不是这个 js/php mishmash。
-
抱歉,好的 - 我更新了我的问题...
-
您真的应该再次评估该代码。你为什么连续做 *THREE
new Date(period_start)?为什么要创建三次相同的对象,尤其是因为您不更改日期值...
标签: javascript jquery json date compare