这对你们大多数人来说看起来很简单,但不适合我,所以任何帮助都会受到赞赏。如何在函数的返回部分更改其中一个变量?我只是添加变量clock =并在下面添加if语句吗?
if (ms > than 86400000) {clock = days + " " + hours};
还是这样?
if (ms > than 3600000) return clock : hours + ":" + minutes.
这是我到目前为止所拥有的。
if (DateDiff("ms", now, TestDue) >= 0) {
var Date1 = new Date();
var Date2 = Date.parse(Date1);
var TestLate1 = Date.parse(TestLate);
var TestDue1 = Date.parse(TestDue);
var TestLate2 = convertMS(TestLate1 - Date2).clock;
var TestDue2 = convertMS(TestDue1 - Date2).clock;
var TestDiff = convertMS(TestLate1 - TestDue1).clock;
tto = "../Images/Blue-120-Button.png";
ttt = "Test Start " + TestDue2;
ttm = "../Images/Blue-120-ButtonMouseOver.png";
ttd = "Test will start in " + TestDue2 + ", will be due in " + (TestDiff) + " after that and will be late on " + TestLate + ".";
C3color = "#FFFFFF";
C3Mcolor = "#FFFF00";
ASTRIS = "../Images/BlueTestB.png";
ASTRIS2 = "../Images/BlueTestB2.png";
这就是我需要为函数实现不同时间的函数的一部分。
function convertMS(ms) {
days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000) / 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1) {
minutes = "0" + minutes
};
if (seconds.toString().length == 1) {
seconds = "0" + seconds
};
//need to change the conditions with if statements for clock//
return {
days: days,
hours: hours,
minutes: minutes,
seconds: seconds,
clock: days + " " + hours + ":" + minutes + ":" + seconds
};
}
答案 0 :(得分:2)
那样:
function convertMS(ms) {
days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000); // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1 ) {minutes = "0" + minutes};
if (seconds.toString().length == 1 ) {seconds = "0" + seconds};
var clock = '';
if(ms > 86400000) {
clock = days + " " + hours;
} else if(ms > 3600000 && ms < 86400000) {
clock = hours + ":" + minutes;
} else if(ms > 60000 && ms < 3600000) {
clock = minutes + ":" + seconds;
} else {
clock = seconds;
}
return {
days : days,
hours : hours,
minutes : minutes,
seconds : seconds,
clock : clock
};
}
答案 1 :(得分:1)
我想你正在寻找三元运营商。这可能会指向正确的方向:
return ms > 86400000 ? days + " " + hours : hours + ":" + minutes
或类似的东西。请查看here以获取更多信息,您应该可以轻松地将其调整为您的代码。
答案 2 :(得分:1)
使用单独的变量来格式化时间,您可以在返回最终对象之前尽可能多地修改它。
function convertMS(ms) {
var days = Math.floor(ms / 86400000), // 1 Day = 86400000 Milliseconds
hours = Math.floor((ms % 86400000)/ 3600000), // 1 Hour = 3600000 Milliseconds
minutes = Math.floor((ms % 3600000) / 60000), // 1 Minutes = 60000 Milliseconds
seconds = Math.floor(((ms % 360000) % 60000) / 1000) // 1 Second = 1000 Milliseconds
if (minutes.toString().length == 1 ) {minutes = "0" + minutes}
if (seconds.toString().length == 1 ) {seconds = "0" + seconds}
var clockFormat;
if( days > 0 ) {
clockFormat = days + " " + hours;
}
else if( hours > 0 ) {
clockFormat = hours + ":" + minutes;
}
else {
clockFormat = minutes + ":" + seconds;
}
return {
days : days,
hours : hours,
minutes : minutes,
seconds : seconds,
clock : clockFormat
};
}