Double digit time discrepancies

I am looking to format the TravelTimeHoursDiff and TravelTimeMinutesDiff in double digits. Currently, the time is displayed as 7:0 and I would like it to be displayed as 07:00.

 if ($scope.DispatchStatus.ArrivalTime != undefined){
                var today = $rootScope.getSysDate().split(" ");
                var timeArrival = new Date(today[0] + ' ' + $scope.DispatchStatus.ArrivalTime);
                var TravelTime = new Date(today[0] + ' ' + $scope.Route.TravelTime);
                var timeArrivalHours = timeArrival.getHours();
                var TravelTimeHoursDiff = timeArrivalHours - TravelTime.getHours() ;
                var TravelTimeMinutesDiff = (timeArrival.getMinutes() - TravelTime.getMinutes());

                    if(TravelTimeHoursDiff < 0 || (TravelTimeHoursDiff <= 0 && TravelTimeMinutesDiff < 0) || (TravelTimeHoursDiff == 0 && TravelTimeMinutesDiff == 0)){                    
                        $scope.formvalidationbit = $scope.DispatchStatusAddForm[fieldName].$invalid = true;
                         angular.element('#' + fieldName).addClass('ng-invalid');
                         angular.element('#' + fieldName).removeClass('ng-valid');
                         $scope.DispatchStatusAddForm.$valid = false;
                         var errorbit = 1;

                    }else{
                        if (isNaN(TravelTimeHoursDiff)) {
                             TravelTimeHoursDiff = '--';
                         }
                         if (isNaN(TravelTimeMinutesDiff)) {
                             TravelTimeMinutesDiff = '--';
                         }
                         if(TravelTimeMinutesDiff <0){
                             TravelTimeMinutesDiff = TravelTimeMinutesDiff * (-1);
                         }
                         $scope.TravelTime = TravelTimeHoursDiff + ':' + TravelTimeMinutesDiff;
                    }
}

Answer №1

To ensure values below 10 have a leading zero, you can implement the following function:

function prependZero(value) {
    return value < 10 ? ("0" + value) : value;
}
$scope.TotalTime = prependZero(TotalHoursDifference) + ':' + prependZero(TotalMinutesDifference);

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

Tips for preventing ng-repeat from refreshing data that has not been affected

How can I effectively address this issue? I am facing a challenge with a list of items displayed on the user's view. Each item has the ability to be expanded, but only one at a time (I'm utilizing Angular Bootstrap Accordion). Inside the expande ...

Accessing this.href from the ASP.NET code behind using Javascript

I'm trying to pass this.href from my asp.net code behind to JavaScript. When I run the code, the value appears as 'undefined' in the alert message. Can anyone help me with this issue? Page.ClientScript.RegisterStartupScript(this.GetType(), ...

What is the best way to limit the date picker to only accept numbers and hyphens in the input field while blocking any other input in Vue?

I have been utilizing the vue2-datepicker npm package for handling dates. The date input currently accepts all alphabets, numbers, and special characters but I only want it to allow numbers, hyphens, and forward slashes. It's simple to achieve this us ...

Double firing of beforeUpdate event observed in Vue for recursive components

Recently delving into the world of vue js, I've been tinkering around with a recursive tree example inspired by the official example found here. While the original example starts with treedata containing a single root and multiple children, my goal w ...

How to Route in Angular 5 and Pass a String as a Parameter in the URL

I am currently working on an Angular project that focuses on geographic system data. The concept is as follows: I have a component with the route: {path: 'home'}. I aim to pass a geojson URL along with this route, making it look like this: {pat ...

Discovering the Voice Channel of a User using Discord.js v13

I'm currently working on a 'wake up' command for my bot that should move the mentioned member between 2 specific voice chats and then return them to their original VC. I've successfully got the bot to move me between those 2 VCs, but no ...

Determining the maximum number within an array using JavaScript

Similar Question: How can I identify the largest number in a JavaScript array? I'm facing issues with this piece of code. Despite spending some time on it, I can't seem to make it work properly. The console only shows 0 when I run it. Can som ...

Tips on customizing Google Analytics code with Google Tag Manager

My website is currently equipped with the Google Analytics tracking code implemented through Google Tag Manager. The standard Google Analytics code typically appears as follows: <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']= ...

Looking to display outcomes on a separate line every time you click? Currently developing a project involving Rock, Paper, Scissors

I have been tackling the Rock Paper Scissors project from The Odin Project. Take a look at the interface I have developed so far: My goal is to display the results on new lines every time a button is clicked. Instead, they are appearing next to each oth ...

sending an array variable to ajax in CakePHP

I am currently working on creating a feature similar to this where users can select multiple conditions for their search. My approach involves populating an array with the selected conditions and passing it through ajax to make a request. Below are snippet ...

Steps for resending an Ajax request once the previous one has been completed

Currently, I am experimenting with a basic Ajax request to a webpage by triggering it through an onclick event on a button: // 1. Create an XMLHttpRequest object var myRequest = new XMLHttpRequest(); // 2. Use the open method to request a resource from th ...

What is the best way to detect an empty string in AngularJS?

When working with a form, I needed to ensure that a string is not empty. If the string is indeed empty, I wanted to set a default value. Otherwise, I wanted to pass the actual value. Below is the code snippet from the controller: $scope.addElem = functi ...

Irritating Popup - (or perhaps a more elegant alternative)

My current challenge involves tracking the time a user spends with a specific window in focus. I aim to record this duration accurately and pause the timer when the user switches to another window, then resume it upon returning. Ultimately, I want to measu ...

Converting Java 8 LocalDate to a JavaScript Date object

Can you help me with converting a Java LocalDate to a JavaScript Date? Here is the data I have: { "date": { "year": 2016, "month": "NOVEMBER", "dayOfMonth": 15, "monthValue": 11, "dayOfWeek": "TUESDAY", ...

What is the top choice instead of using the jQuery toggle() method?

After jQuery deprecated the toggle() method, I began searching for alternative ways to toggle classes on Stack Overflow. I came across various other methods to accomplish the same task (Alternative to jQuery's .toggle() method that supports eventData? ...

The browser fails to render an EJS-generated page when responding to a Fetch request

Today, I encountered a strange issue that has left me puzzled. In summary, I have been sending a PUT request from a user form to the backend. Since forms do not natively support PUT/DELETE requests, I had to use a script to handle this task for me. let for ...

Pagination system updates the associated model's value to 1

Is there a way to prevent the pagination control from changing the model value to 1 on initial load? You can find a Plunker showcasing this issue here. <!DOCTYPE html> <html> <head> <link href="https://maxcdn.bootstrapcdn.com/bootstr ...

Only make an AJAX request for suggestions with jQuery Autocomplete if the item does not match any data from the previous request

I am currently working with jQuery Autocomplete functionality. The issue I am facing is that it triggers an AJAX request every time a key is pressed, which is not desirable. Ideally, if the data from a previous AJAX request already matches the search query ...

How to access a $scope variable in the same Angular controller function from outside the function

As a newcomer to AngularJS, I have a question about accessing my $scope variable from an outside function within the same controller. How can I achieve this? Below is the code snippet: .controller('RekapCtrl', ['$scope', '$timeout ...

Error: Cannot call method rewriteRequestUrl in _this7._config.server in React native version 0.64.1

After diligently following all the guidelines provided at the upgrade helper for transitioning from RNv0.62 to v0.64.1, I managed to successfully install my app on the device. However, my metro bundler is encountering an obscure error that has left me perp ...