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

  • SEARCH
  • Home
  • 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 1111789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:36:14+00:00 2026-05-17T02:36:14+00:00

I want to pick up date from screen, add 1 day and put back

  • 0

I want to pick up date from screen, add 1 day and put back to screen.

<input type='text'value='20101231' id='from_date'>

<script>    

    function date_move(direction){              
        var from_date = document.getElementById('from_date').value;                 
        var YYYY = from_date.substring(0,4);
        var MM = from_date.substring(4,6);
        var DD = from_date.substring(6,8);      
        var jsCurDate = new Date( parseInt(YYYY,10), parseInt(MM,10)-1, parseInt(DD,10) );
        var newDate = addOrSubtracDays(jsCurDate,direction)             
        document.getElementById('from_date').value = newDate;   
    }


    function addOrSubtracDays(jsCurDate,daysOffset){        
        var daylength= 1*24*60*60*1000;
        var newDate = new Date(jsCurDate + daylength);
        alert(newDate)      
    }


date_move(+1);

</script>   
  • 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-17T02:36:14+00:00Added an answer on May 17, 2026 at 2:36 am

    You can’t use element.value to get the contents of an arbitrary HTML element, .value only works on form elements. Use element.innerHTML instead:

    var from_date = document.getElementById('from_date').innerHTML;
    

    Also, your use of substring is wrong. I think substr is more clear:

    var YYYY = from_date.substr(0,4);
    var MM = from_date.substr(4,2);
    var DD = from_date.substr(6,2);
    

    In addOrSubtracDays (shouldn’t that be addOrSubtractDays?) you want the number of milliseconds, so you should call Date.getTime. Moreover, you actually want to return the new value:

    return new Date(jsCurDate.getTime() + daylength * daysOffset);
    

    When changing #from_date you probably want your date to be in the same format. For this, I extend Date‘s prototype with a new method (and String with a helper function):

    String.prototype.padLeft = function (length, character) {
        return new Array(length - this.length + 1).join(character || ' ') + this;
    };
    
    Date.prototype.toFormattedString = function () {
        return [String(this.getFullYear()),
                String(this.getMonth()+1).padLeft(2, '0'),
                String(this.getDate()).padLeft(2, '0')].join('');
    };
    

    After this, we can simply call toFormattedString:

    document.getElementById('from_date').innerHTML = newDate.toFormattedString();
    

    BTW, you don’t need to explicitly convert YYYY, MM and DD to integers with base 10; those are converted automatically (without assuming a string beginning with 0 is an octal number):

    var jsCurDate = new Date(YYYY, MM - 1, DD);
    

    For the sake of clarity, the complete script now reads:

    String.prototype.padLeft = function (length, character) {
        return new Array(length - this.length + 1).join(character || ' ') + this;
    };
    
    Date.prototype.toFormattedString = function () {
        return [String(this.getFullYear()),
                String(this.getMonth()+1).padLeft(2, '0'),
                String(this.getDate()).padLeft(2, '0')].join('');
    };
    
    function date_move(direction){
        var from_date = document.getElementById('from_date').innerHTML;
        var YYYY = from_date.substr(0,4);
        var MM = from_date.substr(4,2);
        var DD = from_date.substr(6,2);
        var jsCurDate = new Date(YYYY, MM - 1, DD);
        var newDate = addOrSubtracDays(jsCurDate, direction);
        document.getElementById('from_date').innerHTML = newDate.toFormattedString();
    }
    
    function addOrSubtracDays(jsCurDate, daysOffset){
        var daylength= 1*24*60*60*1000;
        return new Date(jsCurDate.getTime() + daylength * daysOffset);
    }
    
    date_move(+1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My asp.net site allows users to pick the theme they want from a list
want to know why String behaves like value type while using ==. String s1
I pick this date from a textbox and i would like to format to
my date format is like 2010-11-15 04:28:31 i want to pick just 2010-11-15 instead
Basically I'm using datepicker from jQuery to have my client pick a date. I
I want to pick random number of inputs randomly in an array of input
i want to pick how many div are in my html. so i used
Lets say I have an array of length n and I want to pick
I'm building a CMS in ASP.NET MVC and want to allow users to pick
Basically, I want to get a list of all installed apps and pick one

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.