Display month & last sunday of month. Help with my code
Hello. I've been struggling with a bit of javascript. I need to display 'current month' and 'date of last Sunday in a month'
Here's what I have so far. I'm getting closer. I can write the date of last sunday of the month but I can't seem to get the current month to display properly.
Example: for this month I want the code to display January 27 but I'm getting December 27. 27 IS the correct date because 27 is the last Sunday in this month but (obviously) I need it to display January because Januuary IS the current month.
Yes, that will do it. Thanks lucky. I was hoping to combine the two bits of code into one script. IE Display current month AND the date of the last Sunday (27) in the current month. Having it in one piece of code will not really be necessary. This will do just fine.
Code:
<script>
document.write("<h1>Lucky is a rockstar!</h1>");
</script>
<script type="text/javascript">
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
var today = new Date(); // or any specified date
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
var dy = lastDayOfMonth.getDay();
var dt = lastDayOfMonth.getDate();
var date = dt-dy;
var suffix = (date==1 || date==21 || date==31) ? "st" : "th" && (date==2 || date==22) ? "nd" : "th" && (date==3 || date==23) ? "rd" : "th";
var result = monthNames[today.getMonth()] + " " + date + suffix;
alert ("The last Sunday of the month is " + result);
</script>
Quizmaster: What was the currency of Holland before the Euro?
Contestant: Was it the Danish Krone?
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
There's only one thing really wrong with the original code:
Quote:
Originally Posted by pilsnermonkey
Code:
d.setDate(0);
This sets the date to the last day of the previous month (why you were getting December instead of January) - to set to the last day of the current month you'd need to add 1 to the month as well.
There's only one thing really wrong with the original code:
This sets the date to the last day of the previous month (why you were getting December instead of January) - to set to the last day of the current month you'd need to add 1 to the month as well.
<script>
document.write("<h1>Phillip M is a Supreme Master coder and a rockstar!</h1>");
</script>
Alas, document.write() is obsolete. And I am afraid that I am not a rock star. I have all the musical talent of Vincent van Gogh.
I only know two tunes. One is "God Save The Queen", the other isn't.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Well if that God Save the Queen is Johnny & Sid's version I know that one too! Thanks for all your help. Got me out of a perplexing issue. document.write obsolete? Oh brother ... it's working for my purposes in this instance so I will not temp fate at this time. Cheers.
document.write obsolete? Oh brother ... it's working for my purposes in this instance so I will not temp fate at this time. Cheers.
Netscape 3 is the most recent browser where there isn't a better way to interact with the web page from JavaScript than to use document.write so it has effectively been obsolete since that browser died 10+ years ago.
With all current browsers you can use either innerHTML which resolves most of the problems that document.write had or you can use the many DOM commands that make up about 60% of JavaScript that even get around the few problems that still exist with innerHTML.
No one should be being taught document.write any more as even browser dinosaurs such as Netscape 4 and IE4 had their own far better ways of interacting with the web page from JavaScript.