Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2013, 03:26 AM   PM User | #1
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
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.


Code:
<script type="text/javascript">
var d = new Date();
var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];

d.setMonth( d.getMonth() + 0 );
d.setDate(0);
lastsunday = d.getDate() - (d.getDay() + 3);

document.write(monthNames[d.getMonth()]);
document.write(" ");
document.write(lastsunday);
</script>
If anyone can help me get that sorted I'd appreciate it.
pilsnermonkey is offline   Reply With Quote
Old 01-18-2013, 04:38 AM   PM User | #2
lucky7
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
lucky7 is an unknown quantity at this point
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to display the name of this month.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";

var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=month[d.getMonth()];
}
</script>

</body>
</html>





Hope this helps
lucky7 is offline   Reply With Quote
Users who have thanked lucky7 for this post:
pilsnermonkey (01-18-2013)
Old 01-18-2013, 05:47 AM   PM User | #3
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
Thumbs up

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>
pilsnermonkey is offline   Reply With Quote
Old 01-18-2013, 07:54 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Code:
<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.

Last edited by Philip M; 01-18-2013 at 08:34 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
pilsnermonkey (01-19-2013)
Old 01-18-2013, 08:49 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
There's only one thing really wrong with the original code:

Quote:
Originally Posted by pilsnermonkey View Post
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
pilsnermonkey (01-19-2013)
Old 01-18-2013, 09:09 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by felgall View Post
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.
In which case it needs to be

Code:
d.setMonth(d.getMonth() + 1);
d.setDate(0);
lastsunday = d.getDate() - (d.getDay());
__________________

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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
pilsnermonkey (01-19-2013)
Old 01-19-2013, 07:59 AM   PM User | #7
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
<script>
document.write("<h1>Phillip M is a Supreme Master coder and a rockstar!</h1>");
</script>
pilsnermonkey is offline   Reply With Quote
Old 01-19-2013, 08:12 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by pilsnermonkey View Post
<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.

Last edited by Philip M; 01-19-2013 at 08:15 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
pilsnermonkey (01-19-2013)
Old 01-19-2013, 08:59 PM   PM User | #9
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
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.
pilsnermonkey is offline   Reply With Quote
Old 01-19-2013, 09:29 PM   PM User | #10
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by pilsnermonkey View Post
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.

document.write is history - not programming.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
pilsnermonkey (01-19-2013)
Old 01-19-2013, 09:51 PM   PM User | #11
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
Thanks for clearing that up felgal. I'd better triple check browser compatibility ... and look into that more.
pilsnermonkey is offline   Reply With Quote
Old 01-19-2013, 09:53 PM   PM User | #12
pilsnermonkey
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 7
Thanked 0 Times in 0 Posts
pilsnermonkey is an unknown quantity at this point
Resolved

Resolved
pilsnermonkey is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:52 PM.


Advertisement
Log in to turn off these ads.