javascriptdatejs

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();. 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});. But it didn't work. Does anybody know how I can accomplish this with this API or plain js or any other 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.

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);

Solution

  • You can check if today is a particular day by using 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>