Is it possible to extract only the time from a date and time using angular-moment library?

I am trying to display only the time from a date. My code looks like this:

$scope.sample_time = "September 14th 2017, 1:00:00 pm";

What I want in my view is to show just the time, for example 1:00 pm.

I attempted to use angular-moment, with something like

{{ sample_time | amParse:'HH:mm a'}}
but the output is 2017-09-14T06:00:00.000Z.

Is there something that I am missing here? Any assistance would be greatly appreciated.

Answer №1

If the date "September 14th, 2017 at 1:00:00 pm" is not recognized as a valid default date, you must first parse it using the specified format ('MMMM Do YYYY, h:mm:ss a'), then you will have a valid date that can be formatted using amDateFormat like this: amDatFormat:'HH:mm a'

{{ sample_time | amParse:'MMMM Do YYYY, h:mm:ss a' | amDateFormat: 'H:mm s'}}

You can see an example on Plunker here: http://plnkr.co/edit/RocNLPq6uBlFnccLFB8N?p=preview

Answer №2

Here is how I approach it:

{{ getFormattedTime(sample_time) | date: "hh:mm a"}}

The function getFormattedTime converts the current time to local time.

 $scope.getFormattedTime = function (time) {

        return new Date(moment.utc(time));
    }

This will display the time in the format you desire. Let me know if you have any better suggestions. Hope this explanation is helpful.

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

Ways to maintain the value of req.session using express-session

Check out these lines of code: const session = require('express-session'); const sessionConfig = { secret: 'somesecretkey', cookie: {secure: false}, resave: false, saveUninitialized: false, store: new mongostore({ mo ...

Incorporating stick-to-top scroll functionality using AngularJS and ng

I am trying to implement a 'sticky' scroll on dynamic content. My current working example can be found here. It seems to be working, but I encounter a small 'flicker' when new items are appended. This issue seems to be related to the se ...

Customizing the appearance of Indicators and Paginator in PrimeNG Carousel

I have integrated a carousel using PrimeNG which has the following design here Take note of the style of the carousel indicators and navigators. I want to achieve the default style of indicators/navigators for the carousel as shown here I have included t ...

Detecting coordinates (x, y) on a canvas

I'm currently working on a mini-game and encountering an issue with the player movement around the green square. My character seems to be unable to move past the x, y coordinates of the square, even though it can approach it closely. I would really ap ...

Issue [ERR_MODULE_NOT_FOUND]: The module 'buildapp' could not be located within buildserver.js

I am currently working on a node project using typescript. The project's structure is organized in the following way: --src |-- server.ts |-- app.ts --build |-- server.js |-- app.js In server.ts: import { app } from &q ...

Navigating the screen reader with the cursor位

Site Design Challenge I recently discovered that the master/detail design of my website is not very accessible. The main view features a column chart where each column represents a different month. Clicking on one of these columns reveals details in a nes ...

Interacting with the Follow/Unfollow button using jQuery/Ajax, managing repetitive actions efficiently

I'm working on developing a Follow/Unfollow button that can toggle between the two functions without requiring a page refresh. It currently works smoothly - when I click "Follow," it adds the follow data to the database and displays the "Unfollow" but ...

Issues with JQuery Ajax rendering in browser (suspected)

I'm encountering an issue on my webpage. I have a div with two hidden fields containing values of 0 and 2, respectively. Upon clicking a button that triggers an AJAX query, the div contents are updated with hidden field values of 1 and 2. However, it ...

Registering a function for chart.js plugins that manipulates external data

Is there a way to pass external data to the chart.plugins.register function? I'm struggling because I can't access the necessary context: Chart.plugins.register( { beforeDraw: function (chart) { //implementation } }); I attempted using ...

What is the reason behind fullstack-angular generator utilizing Lo-Dash's merge rather than document.set?

This is the original code snippet used for updating: exports.update = function(req, res) { if(req.body._id) { delete req.body._id; } Thing.findById(req.params.id, function (err, thing) { if (err) { return handleError(res, err); } if(!thing) { ...

Getting the chosen option from a dropdown list mapped in ReactJS

I am working on a dropdown select option that is linked to the data of an array object called 'template_titles'. Currently, the value in the dropdown corresponds to the title in the object. My goal is to be able to extract and use the selected va ...

Is there a way for me to dynamically incorporate new elements into this object?

I am working with an object msg.data that contains the following information: { "user_id": 1, "success": "true" } Now, I want to add a name field to this list: { "user_id": 1, "success": "true", "name" : "giri" } I have attempted the following ...

Display sub-objects within Chart.js

I'm currently tackling a project in Ionic 3 where I am utilizing an API that returns a JSON response with nested objects. My goal is to display certain aspects of these objects within a bar graph using chart.js. Unfortunately, I lack experience in ma ...

Conditionality in the ng-repeat directive of AngularJS

Could someone please help with setting a condition in ng-repeat inside a custom directive call? <map-marker ng-repeat='obj in objects' title= 'obj.name' latitude= 'obj.last_point().latitude' longitude= ' ...

Testing controllers in AngularJS with units

Unit testing in AngularJs has been a bit confusing for me as I am just getting started with it. The syntax seems unusual to me, especially when writing tests for a controller. Here is an example code snippet: describe('PhoneCat controllers', fun ...

A proposal for implementing constructor parameter properties in ECMAScript

TypeScript provides a convenient syntax for constructor parameter properties, allowing you to write code like this: constructor(a, public b, private _c) {} This is essentially shorthand for the following code: constructor(a, b, _c) { this.b = b; thi ...

Implement a basic JavaScript prompt feature within a Node.js application that can be integrated into

My Angular App is wrapped by electron and utilizes node.js to fetch MySQL data for AngularJs via electron. However, since there is no fixed database in my project, I have to dynamically change the database credentials from the client side, making sure it p ...

There seems to be a problem retrieving the JSON file

My JavaScript file is designed to fetch a JSON file and execute a function if successful. If there's an error, it should display an alert window with the message "error". However, despite checking the syntax, I keep receiving an error alert every time ...

How can you handle undefined values in the query object parameter when using Mongoose's Find function?

Alright: Sound.find({ what: req.query.what, where: req.query.where, date: req.query.date, hour: req.query.hour}, function(err, varToStoreResult, count) { //perform some actions }); Let's consider a scenario where only req.query.what has a ...

Encountering an error while setting up the object spread operator Babel plugin for ES201

Exploring the possibilities of the new ES2018 spread operator for objects led me to discovering a promising NPM package: babel-plugin-transform-object-rest-spread Here's a glimpse of my package.json: // Scripts section "scripts": { "dev": " ...