Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222558
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:00:18+00:00 2026-05-17T16:00:18+00:00

I’m having an issue when working with javascript date objects, specifically, when trying to

  • 0

I’m having an issue when working with javascript date objects, specifically, when trying to add a day to a date object. The function I’m writing takes a start date and creates a list of the next seven days, used as the headers of a “week view” calendar app. When the week dates it’s determining are within the same month, my function works as expected. When moving to the next month, things get wonky.

My date list generating function (NOTE: the .toHdr() method is a custom date method I’ve created, as is .clone()):

getWeekDates : function(date){                                  
    /*                                                          
     * Given a date object, returns a list of dates strings that
     * become the header of the week view                       
     */                                                         
    var _d = date.clone(), dates = [];                          
    for(var i=1; i<=7; i++){                                    
        dates.push(_d.toHdr());                                 
        _d.setDate(date.getDate() + i);                         
    }                                                                             
    return dates;                                               
}     

When the date passed to this function is something like “Monday October 11th” (as a date object), the function above returns :

["Monday - Oct. 11, 2010", "Tuesday - Oct. 12, 2010", "Wednesday - Oct. 13, 2010", "Thursday - Oct. 14, 2010", "Friday - Oct. 15, 2010", "Saturday - Oct. 16, 2010", "Sunday - Oct. 17, 2010"]

However, when passed a date like “Sunday October 31st”, the list that is returned looks like:

["Sunday - Oct. 31, 2010", "Monday - Nov. 1, 2010", "Friday - Dec. 3, 2010", "Monday - Jan. 3, 2011", "Friday - Feb. 4, 2011", "Tuesday - Mar. 8, 2011", "Wednesday - Apr. 6, 2011"]

Obviously adding one day to at a time to a date object become problematic when moving to another month, I’m just not exactly sure how to get around it. Any ideas?

EDIT:

Date.prototype.toHdr = function(){
    /*
     * Convenience method for creating a formatted string that will be used in
     * all headers with a specific date.
     */
    var dayMapping = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    var monthMapping = ['Jan.','Feb.','Mar.','Apr.','May','June','July','Aug.','Sept.','Oct.','Nov.','Dec.'];
    return dayMapping[this.getDay()] + ' - ' + monthMapping[this.getMonth()] + ' ' + this.getDate() + ', ' + this.getFullYear();

};

Date.prototype.clone = function(){
    /*
     * Clone a date object, useful for creating a copy of a date, so the
     * original isn't modified.
     */
    return new Date(this.getTime());
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:00:18+00:00Added an answer on May 17, 2026 at 4:00 pm

    Inside the loop you’re using the supplied date as the reference point for every setDate():

    _d.setDate(date.getDate() + i);
    

    When date is the last day of October (31), and i is 1, this sets _d day-of-month to the 32nd – i.e. the 1st November. On the next iteration you set the day of month of _d to (31+2), but remember _d is now in November – so the date moves forward to 3rd December. In subsequent iterations your step size is increasing by an extra day each month, hence your result. Could you call the getter on _d instead? In that case instead of setting day-of-month to 32,33,34, etc. days, you would be setting to 32, then to 2, 3, and so on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.