我正在使用javascript,如何从变量var currentTime = "11:25:00";
var nextTime = "11:35:00";
if(currentTime <= nextTime)
{
alert("test");
}
检查当前时间是否低于10分钟?
nextTime
如果我喜欢上面的代码,时间是11:35:00,它总是提醒我。
我希望在<input type ="button" value="Install" onclick="location.href='@Url.Action("deploy", "DeployController")'" />
之前10分钟检测到的内容,因此它会始终提醒一次。
有什么想法吗?
答案 0 :(得分:1)
您可以使用moment.js执行此操作。这是示例
var currentTime = "11:25:00";
var nextTime = "11:35:00";
var startTime=moment(currentTime, "HH:mm:ss");
var endTime=moment(nextTime, "HH:mm:ss");
var diffrent = moment.duration(endTime.diff(startTime));
var hours = parseInt(diffrent.asHours());
var minutes = parseInt(diffrent.asMinutes())-hours*60;
var totalMin=(hours*60)+minutes
if(totalMin==10){
alert(true)
}else{
alert('Minutes is != 10 minutes!')
}
&#13;
<script src="https://cdn.jsdelivr.net/npm/moment@2.19.1/moment.min.js"></script>
&#13;
我认为这会对你有所帮助。
答案 1 :(得分:1)
// convert hh:mm:ss string to seconds
const hhmmssToSeconds = ( timestr ) => {
const time = timestr.split(':');
time[0] = parseInt( time[0] ) * 3600; // hours to seconds
time[1] = parseInt( time[1] ) * 60; // minutes to seconds
time[2] = parseInt( time[2] ); // seconds :)
// return sum of seconds
return time.reduce( (acc, val) => acc + val );
}
// difference in seconds
const timeDifference = ( timeA, timeB ) => {
return hhmmssToSeconds( timeA ) - hhmmssToSeconds( timeB );
}
const isTenMinutes = ( timeA, timeB ) => {
return timeDifference( timeA, timeB ) === 600;
}
console.log( isTenMinutes( "11:35:00", "11:24:00" ) ); // false
console.log( isTenMinutes( "11:35:00", "11:25:00" ) ); // true
console.log( isTenMinutes( "11:35:00", "11:26:00" ) ); // false
答案 2 :(得分:0)
//Define the epoc Date
var epocTime="Thu Jan 01 1970";
(Date.parse(epocTime + " " + nextTime) - Date.parse(epocTime + " " + currentTime))/(1000*10*60) --> should be >= 1
答案 3 :(得分:0)
你可以尝试一下,
<script>
var currentTime = "11:25:00"
var nextTime = "11:35:00";
var date1 = new Date("2000-01-02T"+currentTime);
var date2 = new Date("2000-01-02T"+nextTime);
if(date1.getMinutes() <= (date2.getMinutes()-10))
{
alert("test");
}
</script>
答案 4 :(得分:0)
以下代码仅在您在指定时间之前刷新浏览器十次时才有效:
var specifiedTime = new Date("November 02, 2017 05:56:00");
var specifiedTimeMinutes = specifiedTime.getMinutes();
var currentTime = new Date();
var currentTimeMinutes = currentTime.getMinutes();
if(specifiedTimeMinutes-currentTimeMinutes==10)
{
alert("test");
}
您是否需要自动制作而无需刷新浏览器?