编写一个名为displayDayNumber(dateString)的函数(不是生成器),它满足以下规则。
var dayNumber =displayDayNumber("Nov 5,2017"); //309
console.log(dayNumber ()); // 310
dayNumber("Oct 31 2017")); //304
dayNumber();//305
到目前为止,我的代码正在使用一次dateString传递值,但是当我在dayNumber中传递新值时,它不会计算。
var dayNumber =function(dateString=new Date().toDateString())
{
var currentString=dateString;
var no=dateString.substring(8,10);
// console.log(no);
return function () {
// console.log("dateString=",dateString);
var b = calculateNumber(dateString) //helper function to calculate day No
{
var no = dateString.substring(8, 10);
var toNo = parseInt(no) + 1;
var thenString = toNo.toString() + ' ';
var replacecharacter = dateString.replace(no, thenString);
dateString = replacecharacter;
return b;
};
return dateString;
};
}
let genDayNumber=dayNumber ("Sat Nov 05 2017"); //309 //working
genDayNumber(); //working
genDayNumber("Wed Oct 31 2017")); //304 not working
genDayNumber(); //not working
答案 0 :(得分:0)
const dayNumber = date => {
let days = new Date(date).getDay();
return arg => {
if(arg){
return days = new Date(arg).getDay();
}
return ++days;
};
};
答案 1 :(得分:0)
您返回的功能不会参与,所以当您在"Wed Oct 31 2017"
传递时,它会被忽略。
您需要修改返回的函数以接受值。
这是一个工作示例
var dayNumber =displayDayNumber("Nov 5,2017"); //309
console.log(dayNumber); // 309
console.log(dayNumber()); // 310
console.log(dayNumber("Oct 31 2017")); //304
console.log(dayNumber())//305
function displayDayNumber(str) {
let date = new Date(str);
let day = dayOfYear(date);
var result = function(str) {
if (str) {
day = displayDayNumber(str);
return day;
} else {
return ++day;
}
}
result.valueOf = result.toString = function() { return day; }
return result;
}
function dayOfYear(now) {
// Taken from https://stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366
var start = new Date(now.getFullYear(), 0, 0);
var diff = (now - start) + ((start.getTimezoneOffset() - now.getTimezoneOffset()) * 60 * 1000);
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
return day;
}