Incorrect format for a date in AngularJS

I have a time input field where I am receiving the date and time format 'Thu Jan 01 1970 12:59:00 GMT+0530 (India Standard Time)', but I only want to display the time. Is there an issue with the time picker in AngularJS? Can anyone help me resolve this?

function HistoryCtrl($scope) {

     $scope.updateForm = function () {
        console.log($scope.booktime);
       //here i am getting Thu Jan 01 1970 12:59:00 GMT+0530 (India Standard Time)
        
    };

    
}
<div ng-app ng-controller="HistoryCtrl">
    
     <div class="col">
            <label class="item item-input item-stacked-label">
            <span class="input-label" >Time</span>
            <input type="time" ng-model="booktime" name="booktime" min="09:00:00" max="21:00:00" required="">
            </label>
            <div class="form-error" ng-messages="projectForm.booktime.$error">
               <div class="form-error" ng-message="required">* Mandatory</div>
               <div class="form-error" ng-message="min">Booking times: 9am - 9 pm</div>
               <div class="form-error" ng-message="max">Booking times: 9am - 9 pm</div>
            </div>
         </div>
   
    <button ng-click="updateForm()">Update</button>
   
</div>

Answer №1

If you're working with Angularjs, you have access to the $filter service which can be used for date formatting.

Here's a sample code snippet to demonstrate how to use it:

function HistoryCtrl($scope, $filter) {

     $scope.updateForm = function () {
        console.log($filter('date')($scope.booktime, 'mediumTime'));          
    };
}

To customize the time format to your liking, you can refer to this link.

I hope this information proves helpful :)

Answer №2

To handle datetime, you have the option of using moment.js.

http://momentjs.com/

Alternatively, for AngularJS, you can use

https://github.com/urish/angular-moment

Date Formatting Examples:

moment().format('MMMM Do YYYY, h:mm:ss a'); // Example: September 15th 2015, 10:30:07 am
moment().format('dddd');                    // Example: Tuesday
moment().format("MMM Do YY");               // Example: Sep 15th 15
moment().format('YYYY [escaped] YYYY');     // Example: 2015 escaped 2015
moment().format();                          // Example: 2015-09-15T10:30:07+05:30
Relative Time
moment("20111031", "YYYYMMDD").fromNow(); // Example: 4 years ago
moment("20120620", "YYYYMMDD").fromNow(); // Example: 3 years ago
moment().startOf('day').fromNow();        // Example: 11 hours ago
moment().endOf('day').fromNow();          // Example: in 13 hours
moment().startOf('hour').fromNow();       // Example: 30 minutes ago
Calendar Time
moment().subtract(10, 'days').calendar(); // Example: 09/05/2015
moment().subtract(6, 'days').calendar();  // Example: Last Wednesday at 10:30 AM
moment().subtract(3, 'days').calendar();  // Example: Last Saturday at 10:30 AM
moment().subtract(1, 'days').calendar();  // Example: Yesterday at 10:30 AM
moment().calendar();                      // Example: Today at 10:30 AM
moment().add(1, 'days').calendar();       // Example: Tomorrow at 10:30 AM
moment().add(3, 'days').calendar();       // Example: Friday at 10:30 AM
moment().add(10, 'days').calendar();      // Example: 09/25/2015
Multiple Locale Support
moment().format('L');    // Example: 09/15/2015
moment().format('l');    // Example: 9/15/2015
moment().format('LL');   // Example: September 15, 2015
moment().format('ll');   // Example: Sep 15, 2015
moment().format('LLL');  // Example: September 15, 2015 10:30 AM
moment().format('lll');  // Example: Sep 15, 2015 10:30 AM
moment().format('LLLL'); // Example: Tuesday, September 15, 2015 10:30 AM
moment().format('llll'); // Example: Tue, Sep 15, 2015 10:30 AM

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

Button that triggers HTML Audio play when clicked

Is it possible to have audio play when Button 1 is clicked, but then pause or stop if Buttons 2 or 3 are clicked instead? <a href="#" class="button1">Button 1</a> <a href="#" class="button2">Button 2</a> <a href="# ...

Is it possible to process HTML and JavaScript as a request in JMeter?

After receiving a response, I noticed that the HTML body contains a hidden form along with an internal JavaScript function to submit the form (view snapshot here). Is there a method in JMeter to execute JavaScript code that directly submits a form? I am ...

What is the best way to personalize the Window.Confirm() dialog in JavaScript?

var val= confirm("Are you sure to cancel?"); The code snippet above will display a popup with two choices - Ok and Cancel, with Ok being the default choice. Is there a way to make Cancel the default choice instead and switch the positions of the ...

The render props on the child component are not appearing on the screen as expected

Recently diving into React Native, I created a stateless component designed to iterate through props that contain objects with arrays of tags. The goal was to extract and display a single tag from each array item. (Refer to the console log in the screensh ...

What is the best way to connect a nested Vuex object to a Vue.js property within the Tiptap text editor?

I'm attempting to connect the nested Vuex state object to the editor property of the tiptap's editor-content component. The structure of the state is as follows: <template> <table :style="{ backgroundColor: element.options.ba ...

Creating an array using Vue.js

When I initialize a Vue data array like this [0: Object, 2: Object];, the console log in Vue shows the array as [0: Object, 1: undefined 2: Object];. This causes issues when iterating through with 'v-for="cell in row.cells"' as it results in tryi ...

Retrieve the URL of the image from an XML document

Figuring out how to extract the image URL from XML files in Android Studio can be challenging. After analyzing 30 RSS Feed XMLs, I discovered that 95% of them contain ".jpg" images with links starting with "http," not "www." Therefore, I am working on a co ...

Is React failing to insert a span element in the client-side code?

First off, I'm a beginner in the world of react and recently tackled this guide on creating a real-time Twitter stream To cut to the chase, I wanted to include a link next to each tweet that followed its text. To achieve this, I simply added props to ...

Is it possible to use jquery to specifically target classes?

I am trying to achieve the following: $('.class.img').css('cellpadding', variable); However, this code does not seem to be working as expected. Despite searching online, I have not been able to find a solution. Any assistance on how to ...

When the md-menu is clicked, the Top Nav shifts upwards along with the mdDialog buttons

Currently, I am in the process of developing a website using AngularJS and ASP.NET, incorporating Angular Material. However, I encountered an issue where scrolling on the page causes the top navigation to move up the body or essentially "disappear" when cl ...

Prevent those pesky popups with this handy script

Even though AdBlock sometimes doesn't block popups, I have a solution in mind. I plan to use Greasemonkey and jQuery to create my own popup blocker. Is it possible to intercept the clicks and determine if a popup is about to open? $('.popupLaun ...

Is all of the app fetched by Next.js when the initial request is sent?

After doing some research online, I learned that Next.js utilizes client-side routing. This means that when you make the first request, all pages are fetched from the server. Subsequent requests will render those pages in the browser without needing to com ...

Error: Cannot execute products.map in React JS because it is not a function

I'm encountering a TypeError: products.map is not a function error while attempting to iterate or map through the objects in my current state. I am fetching products from an API and storing them in state with the intention of displaying these objects. ...

Searching and replacing query strings in a URL using JQuery according to the chosen option in an HTML dropdown menu

Is there a way to use jQuery to dynamically change a specific value in the query string by finding and replacing that value based on the selection made from a dropdown menu on the webpage? For example: Imagine we have this query string on the current page ...

The jQuery .on("change") function keeps triggering repeatedly, but I'd like it to only execute once

I'm currently working on a feature that involves detecting changes to input text fields. Every time the user makes a change in an input field, I need to capture the new value and validate it. However, I've noticed that the code I have implemented ...

Looking for a way to make the spinner disappear in puppeteer while waiting?

What is the most effective way to wait for the spinner to vanish before clicking on a button? Check out this example ...

Storing information in Firebase using React.js

When storing an object in Firebase, I expected the structure to be as shown in the image below. However, what I received was a generated running number as a key. This is the code I used to store the object in Firebase: var location = []; location.push({ ...

Formatting specific elements within an array

I am working with an array of names, some of which are repeated. These names are then split in half and displayed as li. One thing I am struggling to figure out is how to style the name Joeyc with text-decoration: line-through; on all instances of .book w ...

Is p-queue designed to utilize multiple threads?

Have you heard of a nodejs module that allows you to limit the number of concurrent promises? Check out this link I'm curious, does this module utilize multiple threads? Here's an example taken from the official page: const {default: PQueue} ...

How can AngularJS display ellipses using the limitTo filter exclusively for strings that surpass the character limit?

Looking to use the limitTo filter on strings? If a string is under 10 characters, it will be displayed as is. But if it's longer than 10 characters, I need to cut it off at the tenth character and display ellipses. Is there an easy way to do this with ...