我需要将日期与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
。为什么我的功能只能运行到3月27日?
最佳答案
这是一条评论,但没有足够的空间,我想对其进行格式化。
在代码中:
var period_start = new Date('2016-02-24'),
period_now = new Date(); // <=== terminates statement
period_end = new Date('2016-11-01'),
//current_date = period_start,
array_of_all_dates = [];
请注意,第二行以分号终止,因此period_end和array_of_all_dates变为全局。这可能不是问题,但谁知道……
另外,当前对ECMAScript 2015的解释是:
new Date('2016-02-24')
应该为2016-02-24T00:00:00Z创建一个新的Date,即它被视为UTC(与ISO 8601和标准本身不一致,因为日期和时间字符串没有像2016-02-24T00:00这样的时区:00被视为本地时间,但删除时间部分及其UTC)。
下一个,
new Date()
创建一个本地*日期,因此如果今天是2016年2月12日,则:
new Date() != new Date('2016-02-12')
对于所有偏移量不是00:00的时区,因为时间值会因时区偏移而不同。
我不清楚上面的内容是否会对您的代码产生严重影响,但是随意混合使用UTC和本地日期并不是一个好主意。
接下来,我不了解以下用法:
var dodaj = parseInt('3');
而不是:
var dodaj = 3;
和:
if (new Date(period_start).getTime() > period_now)
无缘无故地创建和丢弃Date对象,以下内容完全相同:
if (period_start > period_now)
这样做的好处是可以避免IE中关于
new Date(date)
和两位数年份的模糊错误。哦,最后,2016年3月27日是在许多地方(例如意大利,但不是美国)daylight saving starts的星期日,所以夏时制可能会干扰您的约会?
*其中“本地日期”表示日期,该日期的时间值已针对主机的当前时区偏移设置进行了调整。
关于javascript - 使用循环和JavaScript比较日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35324876/