Develop a custom time input mask in AngularJS controller

In my AngularJS controller, I have the following code snippet:

$scope.detailConfig = [{
     title: $filter('translate')('bundle.app.HORA_MINUTO_INICIAL_DESCONSIDERAR'),
     property: 'faixaHorariaInicial',
     type: 'text'
 },{
     title: $filter('translate')('bundle.app.HORA_MINUTO_FINAL_DESCONSIDERAR'),
     property: 'faixaHorariaFinal',
     type: 'text'
 }];

Using this code, I am rendering two input fields. I want to add a time mask to these inputs so that users can only enter the time in the format hh:mm.

Can you please guide me on how to achieve this?

Answer №1

When working with an input without tags in JavaScript, it can be a bit challenging to determine what you're dealing with. However, if you have a Date object, you can utilize the following function to output a string in the format "hh:mm" as per your requirement.

var myDate = new Date();

function getTime(d) {
    hours = d.getHours();
    minutes = d.getMinutes();
    return hours + ":" + minutes;
};

var myTime = getTime(myDate);
// this will yield "10:59"

If you are actually using an html Input, I suggest exploring one of the solutions mentioned here. This method will ensure that the input aligns with the value specified in the $filter within your controller. For your scenario, you could implement it as follows:

$scope.tToDisplay = $filter('date')($scope.timestamp, 'HH:mm');

and

<input ng-model="tToDisplay"></input>

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

What are the steps to resolve a peer dependency problem with npm?

I am facing a conflict in my package.json file with the following modules: react-router requires react 0.13.x redbox-react requires react@>=0.13.2 || ^0.14.0-rc1 After running npm install react, I ended up with version <a href="/cdn-cgi/l/emai ...

Using HTML and JavaScript to choose relatives from the extended family: Uncles and Aunts

Looking for a better way to target elements in your HTML code? <div class="chunk" id=""> <div class="chunkContent"> <div class="textLeft" ></div> <div class="textRight" ></div> <div class= ...

Having difficulty implementing two-way binding on SELECT element within Angular JS programmatically

Struggling with implementing two-way binding for SELECT elements. Attempting to dynamically change the selected element through code. While I've come across examples on Stackoverflow for binding the change event for SELECT, finding resources on the ap ...

storing negative floating point numbers in an array using regular expressions in JavaScript

I am currently working on creating a regular expression that can detect both positive and negative floating point numbers of any length. My goal is to store the captured values in an array as double data type. I have attempted the following pattern: input ...

Utilizing a combination of CSS and JavaScript, the traffic light can be altered to change based on

**I stumbled upon this online code snippet where a timer is controlled in CSS. I'm trying to figure out how to control it with JavaScript, but all my attempts have failed so far. Is there anyone who can help me solve this issue? What I'm attempti ...

Adding parameters to a URL is a common practice

"Adding additional information to a URL that was previously included?" I apologize for the confusing title, but I can't find a better way to phrase it. Perhaps an example will make things clearer. Let's say I have URL 1: http://example.com/?v ...

Is there a way to simulate pressing a keyboard button using jQuery?

Below is the code snippet: $("input").on({ keydown: function(ev) { if (ev.which === 27){ // esc button backspace_emolator(); } else if (ev.which === 8){ // backspace button console.log('backspace button ...

Incorporating TinyMCE into numerous dynamically generated text areas

I am facing an issue with dynamically created textareas. The content in these textareas is generated dynamically. This is how I retrieve the data and create the textareas dynamically: $(document).ready(function(){ $('#btn').click(function(){ ...

How to Initialize Multiple Root Components in Angular 4 Using Bootstrap

We are working on a JQuery application and have a requirement to integrate some Angular 4 modules. To achieve this, we are manually bootstrapping an Angular app. However, we are facing an issue where all the Angular components are loading simultaneously wh ...

Tips for crafting services using $q and $http requests while avoiding redundancy

Is there an elegant way to write AngularJS services without repetitive $q syntax? I currently write services like this: (function() { function ServiceFactory($q, $timeout, $http) { return { getFoo: function() { va ...

Issue with $watch function causing $scope variable to remain undefined, even though it is being explicitly

I've been encountering an issue while trying to insert data into my Firebase database using a user's uid. It seems to be coming out as undefined for some reason. Here's a closer look at the code snippet causing the problem: The primary cont ...

Unleashing the power of React: Integrating raw HTML <a href> tags with JavaScript

I am currently working on a small web app that mimics browsing WikiPedia by fetching raw HTML content of WikiPedia articles through its API. I then display this HTML in my app using "dangerouslySetInnerHTML". I am faced with the challenge of enabling a us ...

How can I trigger the execution of textarea HTML code with an onClick event using JavaScript?

When my fingers are lifted from the keyboard, some code will be automatically executed. I want to trigger this function when I click a button (onclick) instead of using onkeyup. <div id="result"></div> <textarea ...

Sending an object and array using an Angularjs service: An easy step-by-step guide

I encountered an issue while attempting to send both an object and an array to an API simultaneously. Here is the object data from input boxes: var vacation = { Vac_Main_Key: $scope.vackey, Vac_Main_Code: $scope.code, Gender_Type: $scope.gen ...

Using raphaeljs and freetransform in conjunction with clip-path

Currently, I am utilizing Raphael along with Elberts FreeTransform Plugin. You can view my progress so far by visiting MyWork The issue arises when I attempt to translate or rotate the set of rectangles within my clip path. Once rotated or translated, it ...

The $injector:modulerr error in AngularJS occurs when dependencies are not loaded before the controller is invoked

Currently, I am facing an issue with loading ng-table in my AngularJs project. It seems that the ng-table script file is being called after the controller script file, resulting in a $injector:modulerr error due to size constraints. Is there a way to ensu ...

Using Google Cloud Function to write and read a JSON file

I'm currently tackling a project that involves comparing two JSON files fetched at different time intervals. Essentially, it's a continuous cron job that retrieves a new JSON response from an API every 20 seconds and compares it with the previous ...

Explore additional sub-categories by clicking on them with the help of jQuery or alternate techniques

I am currently working on a Magento store that has Categories with an overwhelming amount of sub-categories, taking up almost half of the page. I want to enhance user experience by implementing a feature that displays a "Load more" button when there are mo ...

Removing a CSS class using JQuery

Within my website layout, I have a div that is dynamically included using PHP. This div is inserted in two different locations, each inside its own parent div. <div id="parent_div"> <div id="contact_details_div" class="contact_details_div sam ...

How can I conditionally disable a button in Vue.js using an if statement?

Can someone help me figure out why all my buttons are getting disabled when I only want one to be disabled? Here is the code where I created a counter with vue.js: <body> <div id="app"> <button @click="co ...