Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 分别选择字符_Javascript_Jquery_Math - Fatal编程技术网

Javascript 分别选择字符

Javascript 分别选择字符,javascript,jquery,math,Javascript,Jquery,Math,我只需要选择天、月和年(分别),然后计算到期日。为什么它不能正常工作?我是否正确使用了substr 你能试试这个吗 var monthDue = $('.due-date').text().substr(0,2); var dayDue = $('.due-date').text().substr(3,2); var yearDue = $('.due-date').text().substr(6,4); 我还建议您查看moment.js,了解日期操作或计算。 $(此)不是您所认为的,请尝试

我只需要选择天、月和年(分别),然后计算到期日。为什么它不能正常工作?我是否正确使用了
substr

你能试试这个吗

var monthDue = $('.due-date').text().substr(0,2);
var dayDue = $('.due-date').text().substr(3,2);
var yearDue = $('.due-date').text().substr(6,4);
我还建议您查看moment.js,了解日期操作或计算。

$(此)
不是您所认为的,请尝试以下方法:

/* caching the relevant text to avoid having to find the same element/text
   multiple times: */
var due = $('.due-date').text(),
    monthDue = due.substr(0,2),
    dayDue = due.substr(3,2),
    yearDue = due.substr(6,4);

// to demonstrate that you're getting the relevant text
console.log(yearDue, monthDue, dayDue);

$('.days-due').each(function(){
    $(this).text(daysUntil(yearDue, monthDue, dayDue));
});
.


标签数学是什么??
/* caching the relevant text to avoid having to find the same element/text
   multiple times: */
var due = $('.due-date').text(),
    monthDue = due.substr(0,2),
    dayDue = due.substr(3,2),
    yearDue = due.substr(6,4);

// to demonstrate that you're getting the relevant text
console.log(yearDue, monthDue, dayDue);

$('.days-due').each(function(){
    $(this).text(daysUntil(yearDue, monthDue, dayDue));
});
function daysUntil(untilDate) {
    var Time = new Date(untilDate).getTime();
    var currTime = new Date().getTime();
    var difference = Time-currTime; 
    return Math.ceil( difference/(1000*60*60*24) );   
}


$('.days-due').each(function() {
    var dateUntil = $('.due-date').text(); // use something more specific
    $(this).text( daysUntil( dateUntil ) );
} );