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 8945001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:08:56+00:00 2026-06-15T12:08:56+00:00

What is the best way in JavaScript to set the date portion of a

  • 0

What is the best way in JavaScript to set the date portion of a JavaScript date object without affecting the time portion?

Lets say you have a date like this
Wed Oct 31 2012 10:15:00 GMT-0400 (EDT)

And you want to update it to be November 1st without changing the time.

At first glance, you’d think this would work,

exampledate.setFullYear(2012);
exampledate.setMonth(10);
exampledate.setDate(1);

But the result is
Sat Dec 01 2012 10:15:00 GMT-0500 (EST)

I think reason for this is that October has 31 days and November has just 30. Since this obviously doesn’t work right all the time. What are my other options? Will I have to create a new date object?

  • 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-06-15T12:09:00+00:00Added an answer on June 15, 2026 at 12:09 pm

    JavaScript Date object setter methods often allow you to (optionally) set multiple date/time components all at once (in addition to the primary component), as appropriate for the situation — this is what you want to do.

    For your use case, the syntax for setFullYear is setFullYear(year,month,day), so in addition to explicitly setting the year (which would cause the month and date to be implicitly updated based on your initial date), you could also explicitly set the month and day while you’re at it. For instance:

    var d = new Date();
    d.setFullYear(2020,10,3);
    

    will return

    Tue Nov 03 2020 13:10:34 GMT-0500 (EST)
    

    See the JavaScript setFullYear() method for more details.

    Because the time components of the Date object (e.g. hours, minutes, seconds, milliseconds) are not dependent on the date portions (year,month,date), your initial time components will be maintained. If you need to then update the time components, you can safely use setHours() or other methods as needed, because no implicit updates to the date components will occur.

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

Sidebar

Related Questions

What is the best way to pause in JavaScript. I have attempted to use
Possible Duplicate: best way to get the key of a key/value javascript object foo
I would like to reload an <iframe> using JavaScript. The best way I found
Whats the best way to get this layout in CSS? imagine that I have
I have multiple booleans defined in Javascript: On toggle I would like to set
I have a set of masks . The masks look like this '09{2,9}n(6)' //read
Possible Duplicate: Help parsing ISO 8601 date in Javascript I have a set of
Can anyone tell me the best way to set a javascript array value to
What's the best way to pass in the value held in a javascript variable
What is the best way to hide all links except for one in javascript?

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.