Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139953

First Tuesday of every Month

$
0
0

Hello I am trying to use the following code which is a mish mash of "lifted code" but it keeps pumping out todays date and time.

I am trying to get the date for the first Tuesday of every month at 19:00.

I am using W3C School Try-it for testing purposes.

<!DOCTYPE html>
<html>
<head>
<script>
function displayDate()
{

var myDate = new Date();
myDate.setHours(19, 00, 0, 0);
myDate.setYear(2013);

myDate.setDate(1);
myDate.setMonth();


//Find Tuesday
var tue = 2;

while(myDate.getDay() != tue) {
    myDate.setDate(myDate.getDate() + 1);

}

document.write(myDate);
</script>
</head>
<body>

<h1>My First JavaScript</h1>
<p id="demo">This is a paragraph.</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html> 

Roy

<==Update==>

I have use your code which works well but need to factor in after the First Tuesday has happened for the next month, I have tried this if statement but breaks.

function getTuesday() {
    var datenow = new Date();
    var d = new Date(),
        month = d.getMonth(),
        tuedays= [];

    d.setDate(1);

    // Get the first Monday in the month
    while (d.getDay() !== 2) {
        d.setDate(d.getDate() + 1);
    }

    // Get all the other Tuesdays in the month
    while (d.getMonth() === month) {
        tuedays.push(new Date(d.setHours(17,00,00,00)));
        d.setDate(d.getDate() + 7);
    }
If (d.getDate() >= datenow.getDate)
{
    d.setMonth(d.getMonth() + 1);
    document.write(tuedays[1]);
}
Else
{
   document.write(tuedays[1]);
}


}

Viewing all articles
Browse latest Browse all 139953

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>