简体   繁体   English

随着时间最近的星期五

[英]Closest Friday, with time

I am using a Date API called date.js and I want to use it for getting the closest Friday Date.today().next().friday(); 我正在使用一个名为date.js的Date API,我想用它来获取最接近的Friday Date.today().next().friday(); . It works and so on but I wanted to specify a time which I would change the next Friday and not the current so I did this Date.today().next().friday().set({hour: 19}); 它可以工作,依此类推,但是我想指定一个下一个星期五而不是当前星期五的时间,所以我做了这个Date.today().next().friday().set({hour: 19}); . But it didn't work. 但这没有用。 Does anybody know how I can accomplish this with this API or plain js or any other API. 有人知道我如何使用此API或纯js或任何其他API来完成此任务。

Clarification 澄清

Clearing thing up, i have a countdown I want it to countdown to the nearest Friday at 19:00, but I can't get the part with the time to work, it just ignores the time, on Friday it just looks for the net Friday and ignores that the clock haven't pased 19:00 yet. 整理一下,我有一个倒计时,我想让它倒计时到最近的星期五19:00,但是我无法拿到上班时间的零件,它只是忽略了时间,在星期五,它只是在寻找净值星期五,忽略了时钟尚未到19:00。

CODE: 码:

var d = new Date();
var dagnr = d.getDay();
var time = d.getHours();

var nextf = Date.today().next().tuesday().set({hour: 19});
var countDownDate = new Date(nextf);

//Random mening för Savar NEJ


// Romdom Mening för Svar JA
var jalist = [
      "Själklart är det de"
    , "Vad fasen skulle det annars vara?"
    , "A fram med chipsen för fan!"
    , "A men det är det ju!"
    , "Ajemän fram netflix"
    , 
];
var jarand = Math.floor(Math.random() * jalist.length);


// Update the count down every 1 second
var x = setInterval(function() {

    // Get todays date and time
    var now = new Date().getTime();
    console.log(countDownDate);
    // Find the distance between now an the count down date
    var distance = countDownDate - now;
    // Time calculations for days, hours, minutes and seconds
    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
    var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // Testa vilken dag och vilken tid det är
    console.log(dagnr)
    if ((dagnr === 1 && time >= 15) || (dagnr === 6 | dagnr === 0)) {
        document.getElementById("demo").innerHTML = jalist[jarand];
    } else{
        document.getElementById("demo").innerHTML = "Nej tyvärr är det " + days + "d " + hours + "h " + minutes + "m " + seconds + "s " + "kvar";
    }

}, 1000);

You can check if today is a particular day by using Date.today().is().tuesday() . 您可以使用Date.today().is().tuesday()检查今天是否是特定的日子。 Just use that with a ternary operator and it should work just fine. 只需将其与三元运算符一起使用,它就可以正常工作。

 var d = new Date(); var dagnr = d.getDay(); var time = d.getHours(); var nextf = Date.today().is().tuesday() ? Date.today().set({hour: 19}) : Date.next().tuesday().set({hour: 19}); var countDownDate = new Date(nextf); //Random mening för Savar NEJ // Romdom Mening för Svar JA var jalist = [ "Själklart är det de" , "Vad fasen skulle det annars vara?" , "A fram med chipsen för fan!" , "A men det är det ju!" , "Ajemän fram netflix" , ]; var jarand = Math.floor(Math.random() * jalist.length); // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time var now = new Date().getTime(); // Find the distance between now an the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Testa vilken dag och vilken tid det är if ((dagnr === 1 && time >= 15) || (dagnr === 6 | dagnr === 0)) { console.log(jarand); } else{ console.log("Nej tyvärr är det " + days + "d " + hours + "h " + minutes + "m " + seconds + "s " + "kvar"); } }, 1000); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM