Receiving Incorrect Input Value for Input Type 'Month'

Currently, I am attempting to retrieve the value of a two-digit month and a two-digit year from an input type called "month."

ISSUE
The format seems correct, but the incorrect date is being returned.

SOURCE CODE:

$("#cardExpireDate").change(function() {
var expireDate = new Date ( $(this).val() );
  //var expireDay = expireDate.getDate();
  var expireMonth = expireDate.getMonth() + 1;
  var expireYear = parseInt(expireDate.getFullYear().toString().substr(2,2), 10);
var expireDateMMYY = ([expireMonth, expireYear]).join('/');
  //alert([expireMonth, expireYear].join('/'));
$("#summaryCardExpireDate").val( expireDateMMYY );
});
@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700,800);
.container {width:400px; margin:0 auto;}
label {float:left; margin:10px 0; vertical-align:middle; line-height:30px;}
input {width:200px; height:30px; margin:10px; 0; vertical-align:middle; font-family:"Open Sans", sans-serif; font-size:15px; border:0; outline:0;}
#cardExpireDate {border:1px solid #999;}
#summaryCardExpireDate {border:0; border-bottom:1px solid #999;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="formField">
    <input type="month" id="cardExpireDate" name="card_expire_date" placeholder="Expiration..." title="Card Expire Date" required="">
    <label class="" for="cardExpireDate">Expiration Date:
    </label>
    <input type="text" id="summaryCardExpireDate">
    <label class="" for="summaryCardExpireDate">Returned Value:
    </label>
  </div>
</div>

CODEPEN LINK:
http://codepen.io/zuhloobie/pen/ggWdLj

Many thanks in advance :)

UPDATE
Here's what I'm observing on Google Chrome: Version 55.0.2883.87 m (64 bit): https://i.sstatic.net/RmfcF.jpg

Answer №1

Upon investigating the date object, it was determined that the date shown is

Sun Jan 01 2017 03:00:00 GMT+0300 (RTZ 2 (зима))
. This indicates that it is indeed the beginning of a new month. It appears that the issue may be related to localization settings and not using UTC time. By simply adding 1 day to the date, the problem could be resolved.

var expireDate = new Date ( $(this).val() );
expireDate.setDate(expireDate.getDate() + 1);

Based on your location in the western hemisphere, this discrepancy with the Date object's creation can be better understood by reading more here: Javascript date object always one day off?

Answer №2

In order to display the month value with two digits, simply update the variable expireMonth:

from: var expireMonth = expireDate.getMonth() + 1;

to: var expireMonth=('0'+(expireDate.getMonth()+1)).slice(-2);

This adjustment should resolve the issue and display the month value in two digits as expected. Thank you! :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Achieving consistent sorting of duplicate values by introducing a secondary variable in JavaScript

When attempting to arrange a set of divs using javascript, the outcome for repeated values can vary: If there are duplicate values, how they are sorted depends on the initial order in the DOM. Is there a way to sort these duplicates so that regardless of ...

Leverage JavaScript to retrieve the formatting of an element from an external CSS stylesheet

Check out this HTML snippet: <html> <head> <link rel="stylesheet" type="text/css" media="all" href="style.css"> </head> <body> <div id="test">Testing</div> <script> ...

The width of the flexbox child item is too narrow for the content it contains

I am facing an issue with the Flexbox child element not aligning correctly with the content width. Here is the code snippet: https://i.sstatic.net/jtCnSuhF.png .pw-event-options { -webkit-box-flex: 0; -webkit-flex: 0 0 280px; -moz-box-flex: 0; ...

using hover/click functionality with a group of DIV elements

I have a group of DIV elements that I want to apply an effect to when hovering over them with the mouse. Additionally, when one of the DIVs is clicked, it should maintain the hover effect until another DIV is clicked. <div class="items" id="item1"> ...

What is the best method to restore a table cell to its original width?

Here's a quick overview of the issue I'm facing: http://jsfiddle.net/k2Pqw/4/ Once I adjust the red td width, I'm struggling to revert it back to its original size. Setting it at 25% doesn't dynamically adjust like the other tds, it r ...

Send information from a jQuery post to a PHP script and then have it appear instantly on the same PHP file

Looking for a solution to post data to a php script called showitemid.php using ajax and open the same script in a thickbox upon clicking a hyperlink? The goal is to display the posted data on showitemid.php. Check out my code below: postitemid.php In th ...

Make sure that a div and a label are perfectly aligned in a horizontal

I am trying to align a label next to a div horizontally in my asp.net form, but I am having trouble accomplishing this. Below is the code snippet that I have been working with. Can someone please help me? Code Snippet: <div class="modal" id="passwo ...

I am having trouble getting Highcarts to load the JSON data

I am currently in the process of utilizing Highcharts to generate a chart. The JSON output I have is in standard format and was generated using PHP's json_encode() function. Below is the JSON output: [["2015-07-13 16:41:05","3"],["2015-08-15 16:41:05 ...

The :not() selector in CSS3 seems to be ineffective on sibling elements

In my simple design, I have specified that text should appear in red unless it is within a footer element. This style is applied successfully to p tags, a tags, and others. However, for some reason, the styling does not work when I target the footer tag. ...

Avoid using "DOT" as an input type for numbers on mobile devices

Currently, I am working with jQueryMobile and PhoneGap. I have encountered an issue that I have been unable to find a solution for. The problem is that I need to prevent users from typing a DOT in a textbox that is specifically meant for entering their age ...

Failed to set audio source to the source tag after receiving the ajax request

When making an ajax request to the database, I receive an audio link in response. The link is accurate in the variable but is not binding in the source tag. Below is the HTML code I am using to play the audio: function play(){ } < ...

Strange gray gradient background suddenly showing up on mobile devices

Encountering an issue with a login form displayed on a sliding modal. The design looks correct on desktop and even in responsive mode with dev tools, but when accessed through mobile Chrome or Safari on an iPhone, an unusual gray rounded box or shadow appe ...

Enhancing dynamic text boxes with jQuery by incorporating additional information

I've recently developed an interactive app that dynamically creates tables based on user input. The app includes a feature where you can specify the number of tables to generate and track the total count of tables added. Currently, I'm exploring ...

python selenium - Element not found on Justdial website

I've exhausted all options: Attempted both implicit and explicit wait methods Used the wait.until function Utilized the Time module I'm currently attempting to locate the "Load more reviews" link on JustDial. Here is the link. Could you please ...

Obtain the content enclosed by HTML tags

Looking to extract text between html tags? Imagine having a list of cities in California, each within paragraph tags. Can you retrieve only the text inside the paragraph tags? <div class="cities"> <div><p>Los Angeles</p><h5 ...

a div sandwiched between two others

I am trying to nest a <div> within another <div> and center it within the parent. Here is my current approach: <div id="redthing"> <div id="whitebox" > </div> </div> The CSS code I am using is as follows: #red ...

Creating a web form with HTML and integrating it with jQuery AJAX

Looking to fetch data from the server through jQuery AJAX on an HTML form and store the response in a PHP string variable. Here is my current code snippet: <form method="post" name="myform" id="myform" action="https://domain.com/cgi-bin/cgi.exe"> &l ...

Tips for fixing the issue of disappearing top margins in elements while using CSS3 Pie in Internet Explorer 7

Hey there! I've been experimenting with a lot of CSS3 effects, but I'm running into issues trying to get the same effects to work on IE 7 and 8. I've been attempting to use CSS3 Pie to help with this. While CSS3 Pie has worked well for som ...

Ways to display dropdown links in a specific sequence?

Is there a way to display the drop-down links in a specific order? The current order is as follows: Link 1 Link 3 Link 2 Here is the HTML Code: <ul class="navbar-nav"> <li class="nav-item dropdown"> <a class=" ...

Vuetify 3 now displays full text in v-autocomplete without an ellipsis

Trying to truncate long text in a v-autocomplete component using Vuetify 3 and text-overflow: ellipsis, but it's not working. Check out the code below: <div id="app"> <v-app id="inspire"> <v-row align="cen ...