[英]show remaining time left after user input
我试图显示用户输入答案后剩余的时间。
所以它应该是这样的。 该课程什么时候开始? 2022-09-05 (用户输入)今天距离课程开始还有32天
我不认为它应该那么复杂,但我不能让它工作,我不断得到 NaN 或者它只是不工作。
我已经检查过MDN ,但我就是不明白。
代码看起来像这样。
function start(timePassedIn) {
return `Today it is ${timePassedIn} days left until the
course starts`;
}
const course = prompt("When does that course start? ");
const starting = start(course);
console.log(starting);
我删除了我在该日期的所有尝试,以便您可以给我新的输入。
感谢我能得到的所有帮助。
你能试试这个。
function start(timePassedIn) {
return `Today it is ${timePassedIn} days left until the
course starts`;
}
function getDateDifference(inputDate) {
const date1 = new Date(inputDate);
const date2 = new Date();
const diffTime = Math.abs(date1 - date2);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
return diffDays;
}
const course = prompt("When does that course start? ");
const starting = start(getDateDifference(course));
日期可以在程序中以两种方式存储:
我们使用字符串作为输入,使用 output,使用日期对象计算两个日期之间的差异。
如果我理解你的正确,你的程序应该
当您尝试对此步骤进行编程时,第 2 步很复杂:
1. 课程开始时询问用户:
`const course = prompt("When does that course start? ");`
course
将包含一个带日期的字符串。 也许您最好分别询问日期和月份?
const month = prompt("When does that course start? Please enter the month as a number");
const day = prompt("When does that course start? Please enter the day as a number");
2.计算距离课程开始还有多少天
a) 将输入转换为日期 object。 b) 获取当前日期的日期 object c) 计算差异并转换为天
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.