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-31-2013, 10:57 AM   PM User | #1
jonnystudent
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
jonnystudent is an unknown quantity at this point
Day, Month, Date and Year

Hi Guy's, Newbie here.

I'm wondering if someone can help me. I'm trying to output the Day, Month, Date and Year but I'm getting into a spot of bother. I think I'm close, but just need to nail this one last part. My script is this.

Code:
function addDaysToDate(days) {
  var day = new Array("Mon","Tue","Wed","Thur","Fri","Sat","Sun");	
  var mths = new Array("Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec");

  var d = new Date();
  d.setHours(d.getHours() + (24 * days));
  
  var currDa = d.getDay();
  var currD = d.getDate();
  var currM = d.getMonth();
  var currY = d.getFullYear();

  return day  [currDa] + " ," + currM + " " + currD + ", " + currY;
}
It was working fine with the months but I wanted to add the days and it's all gone to pot, can someone help me please. Thanks.

Jonny
jonnystudent is offline   Reply With Quote
Old 01-31-2013, 11:22 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,038
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Days in Javascript run from Sunday (0) to Saturday (6).
Code:
<script type="text/javascript">

function addDaysToToday( numDays ) {
    var date = new Date();
    date.setDate( date.getDate() + numDays );
    var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    var day = ["Sun", "Mon","Tue","Wed","Thur","Fri","Sat"];
    var dy = date.getDay();
    return day[dy] + " " + months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
}

alert (addDaysToToday(7));

// To subtract days from today, just pass a negative number to the function. 

</script>

One of the opposums was St.Matthew who was also a taximan.
- Pupil's answer to Catholic Elementary School test.
__________________

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:
jonnystudent (01-31-2013)
Old 01-31-2013, 11:32 AM   PM User | #3
jonnystudent
New to the CF scene

 
Join Date: Jan 2013
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
jonnystudent is an unknown quantity at this point
Aha, thank you. Works a treat!
jonnystudent 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 01:51 PM.


Advertisement
Log in to turn off these ads.