`Troubleshooting odd behavior with date selection in AngularJS`

When I try to set the default date as the maximum date in a date input field, something strange happens. Initially, I select today's date as the value and everything works fine. However, if I clear the date by clicking on an 'x' icon, the value reverts to the default value of '1970-01-01'. When I try to select today's date again, my JavaScript code checks if the value is undefined and sets it back to the default value of '1970-01-01'. I'm puzzled as to why I can't select today's date after clearing it. This issue occurred when testing in a Chrome browser.

Here is the HTML code:

<!doctype html>
<html ng-app="app">
<head>
<script src="angular/angular.min.js"></script>
</head>
<body>
      <div ng-controller="MainCtrl">
          <input type="date" max="{{today}}" ng-model="myDate.date" ng-change="change()">
      </div>
<script src="app.js"></script>
</body>
</html>

JavaScript code:

var app = angular.module('app', []);

app.controller('MainCtrl', ['$scope', function ($scope) {
    $scope.myDate = {
    date:null
}
$scope.today = '2016-03-14';
$scope.change = function(){
    if(!$scope.myDate.date){
        $scope.myDate.date = new Date('1970-01-01');
    }
}
}]);

Please refer to the picture for more clarity.
View the live demo here.

Answer №1

My suggestion would be to avoid using Chrome's default datepicker because it renders the input box unusable in Firefox.

As for the problem you are facing:

When I input 2016-03-14, the output date displayed is 2016-03-13T23:00:00.000Z (likely due to my timezone).

After resetting the input by clicking the "x", the date shown reverts to 1970-01-01T00:00:00.000Z.

Upon attempting to select 2016-03-14 again, it defaults back to 1970-01-01 because the updated date exceeds the allowed maximum input based on my timezone.

To resolve this issue, including the timezone in new Date('1970-01-01') should rectify the problem. In my case, using

new Date('1970-01-01T23:00:00.000Z')
allowed me to successfully choose 2016-03-14.

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

Populate a database with information collected from a dynamic form submission

I recently created a dynamic form where users can add multiple fields as needed. However, I'm facing a challenge when it comes to saving this data into the database. You can view a similar code snippet for my form here. <form id="addFields" me ...

The Vue.js development server compiler is hitting a roadblock at 49% progress

My Vue JS project keeps getting stuck at 49% or sometimes 62% when I run npm run serve. No matter how long I wait, it never seems to move past that point. I have searched online many times for a solution to this issue, but it appears that no one else is e ...

Troubleshooting the 'App Already Bootstrapped with this Element' Error in AngularJS

When I try to load my AngularJS app, I encounter the following error: Uncaught Error: [ng:btstrpd] App Already Bootstrapped with this Element '<html lang="en" ng-app="app" class="ng-scope">' I have only placed ng-app once in the html elem ...

What could be the reason for the absence of an option in the navbar

As I work on creating a navbar menu that functions as an accordion on desktop and mobile, I encountered an issue. When I click on the dropdown on mobile, it displays one less item than intended. This seems to be related to a design error where the first it ...

In search of a render function that can effectively apply styles to HTML strings using React JS

I have been struggling to convert the result field value, including HTML attributes string, into respective styles for one column in my antd table. Below is the string I am trying to convert: The exhaust air requirements for fume hoods will be based on m ...

Redirecting to an Unverified Website

I encountered an issue in my service.ts file where VeraCode code scan is failing Flaws by CWE ID: URL Redirection to Untrusted Site ('Open Redirect') (CWE ID 601)(16 flaws) Description The web application is vulnerable to URL redirection attacks ...

Javascript Events with a Time Limit

Is there a way to create timers in JavaScript that can trigger certain events in the code and be used as conditions elsewhere without relying on libraries? I need these timers to control the timing of events within the program, such as triggering actions ...

jQuery Counter Effect encountering isNaN error when trying to display a number retrieved from a get API object

I am looking to display the numerical data retrieved from an API using JSON. I want to incorporate a counter effect that displays "isNaN" if necessary. The API URL returns an object with the total number saved in data.data. Can anyone assist me with achi ...

I'm a beginner in Python and could use some clarification on how functions work with input

Can someone help me figure out how to use the input function in a way that allows a function to determine if a number is even or odd without using return statements? def check_even_odd(n): if n % 2 == 0 : print ("even") else: prin ...

The issue of Ionic Angular JS navigation item failing to navigate

I have been able to navigate to other pages in my application without any issues. However, I recently added a new tab and when I click on it, instead of taking me to the page, it simply closes the menu. I'm having trouble figuring out why this is happ ...

jQuery ajax response html not being updated in div on webpage

I have been struggling with this issue for days and I can't seem to find a solution. My goal is to update a link on a web page with a new file. Even though the post request and new file are visible in the Google Chrome development network, the actual ...

The vanishing and reappearance of text within a text box

<asp:TextBox ID="TextBox2" runat="server" value="Your Text here" onfocus=" this.value = '';" onblur="if(this.value == '') ****{ this.value='?????'; }****"></asp:TextBox> I have implemented the functionality that m ...

The navigation bar in React Router is interfering with the loading of other components

Currently, I am in the process of setting up a simple navigation bar that consists of basic buttons without any complex functionality. However, I have encountered an issue where placing the navbar inside the switch prevents other components from loading ...

Navigating through elements in the hidden shadow DOM

Can elements within the Shadow DOM be accessed using python-selenium? For example: There is an input field with type="date": <input type="date" name="bday"> I want to click on the date picker button located on the right and select a ...

Calculation of number combinations

I've been assigned a task that involves generating all possible combinations of numbers from 0-0-0 to 100-100-100 (x, y, z) let x = 0; let y = 0; let z = 0; while(true){ x++; while(true){ y++; while(true){ z++; ...

What is the best way to retrieve data from a database and display it in a select dropdown list

I'm working on creating a Bingo game and I want to include a dropdown list that displays my previous records. How can I retrieve data from a database to populate this select dropdown list? Below is the relevant code snippet in PHP: <html> < ...

Changing the active class on Bootstrap nav tabs across pages

My website features a set of navigation tabs on the homepage that provide links for users to access more information. I want the site to automatically navigate to the corresponding tab on another page when a user clicks on the link. If you'd like to c ...

How to delete the Location Hash from a URL with JavaScript or jQuery

I have encountered a unique issue not addressed in previous discussions. When the destination id is on a different page, I am experiencing difficulty removing #url. My Approach : On two HTML pages - index.html & about.html, I have displayed various menu ...

Please hold off on confirming your RSVP until further interactions have taken place

I have a feature where a component triggers an action when swiped, sending it upwards to the parent route/controller for handling some ajax functionality. The component includes UI elements that indicate a loading state while the action is being processed. ...

The image on my aspx page is not showing up after attaching a master page. Could the issue be something other than the ID not being changed?

I implemented the following code to remove the system-generated ID:- Banner.ascx.cs protected override void Render(System.Web.UI.HtmlTextWriter writer) { StringWriter Html = new StringWriter(); HtmlTextWriter Render = new HtmlTextWriter(Html); ...