Changing Datetime to Date in AngularJS Controller

My goal is to extract only the date from a DateTime object. The variable $scope.mytime holds both the date and time, but I am interested in just the date portion formatted as yyyy-MM-dd.

    $scope.mytime = new Date();

I have attempted a few methods without success. For example:

$scope.mytime.getdate();

This code snippet only returns 8, whereas I require the output in the format of yyyy-MM-dd.

Another attempt was made using:

 $scope.newTime = $filter('date')(new Date(), "dd-MM-yyyy");

This resulted in an error stating: $filter is not defined.

Answer №1

Incorporating filters in the controller is essential for proper functionality, and can be achieved by injecting them like so:

function appController($filter,$scope){

  $scope.newTime = $filter('date')(new Date(), "dd-MM-yyyy");

}

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

appController.$inject=['$filter','$scope'];

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

Space between flex content and border increases on hover and focus effects

My code can be found at this link: https://jsfiddle.net/walshgiarrusso/dmp4c5f3/5/ Code Snippet in HTML <body onload="resize(); resizeEnd();"> <div style="margin: 0% 13.85%; width 72.3%; border-bottom:1px solid gray;"><spanstyle="visibilit ...

What is the method for incorporating PHP's header() function within PayPal JavaScript code?

I'm currently working on integrating Paypal with my website and I've run into some issues handling the various messages sent by Paypal, such as success, failed, and cancelled transactions. Below is a snippet of the Paypal JS code where it manage ...

Having issues retrieving and utilizing response status code following the initial then() method in a Promise

My goal is to utilize the response status code, which is initially available in the first then function along with the JSON response data. However, I am encountering a SyntaxError: Unexpected token U in JSON at position 0. Here is the snippet of the promi ...

Ways to clear an individual form field using element-ui

I'm currently learning VueJS and incorporating the Element-UI framework into my project. One issue I'm facing is that when there's a validation error upon submitting the login form, I'd like to automatically clear the password field. A ...

Modify the ng-repeat item's value when clicked

<div class="info-card"> <p ng-repeat="card in cardList | filter : search" class="{{card.activated == 'true' ? 'active' : 'inactive'}}" > <span>{{card.title}}</span> <a ng-click="card. ...

Naming a JSON object twice

As a newcomer to node.js, I find myself struggling with understanding the process. Can someone please guide me on where I might be going wrong? This is how I create a new Product exports.postAddProduct = (req, res, next) => { const product = new Produ ...

Searching for instances of code patterns in Node.js using regular expressions

I am working with a regex pattern that looks like this... /user/([A-Za-z0-9]*) When applied to the input string below, it produces the following result in console... ['/user/me', 'me', index: 0, input: '/user/me'] Here is ...

AngularJS dynamically creates an HTML template that includes an `ng-click` attribute calling a function with an argument

Struggling to create an HTML template using an AngularJS directive, the issue arises when trying to pass an object into a function within one of the generated elements. Here is the directive code in question: app.directive('listObject', function ...

Unusual JavaScript Bug: Uncaught TypeError - Unable to access property 'url' of null

I encountered a peculiar JavaScript error. The following message appears in the console: Uncaught TypeError: Cannot read property 'url' of null (Line 83) The code on Line 83 looks like this: var image = '<img class="news_image_options ...

Tips for creating a new tab or window for a text document

Below code demonstrates how to open a new tab and display an image with a .jpg extension. Similarly, I want to be able to open a text document (converted from base64 string) in a new tab if the extension is .txt. success: function(data) { var ...

Exploring the $scope variable in AngularJS using JavaScript

How can I assign values to $scope.dragged and $scope.dropped in a JavaScript function? function drag(e){ e.dataTransfer.setData("text/html", e.target.id); console.log(e.target.id); $scope.dragged = e.target.className; } function drop(e){ ...

Problem with Raphael Sketch and Request to Ajax

Utilizing Raphael.js and jQuery Ajax, I am attempting to display some dots (circles) on the map in this [Demo][1]. I have a PHP file called econo.php which looks like this: <?PHP include 'conconfig.php'; $con = new mysqli(DB_HOST,DB_USER,DB_P ...

Skipping validation while navigating back on SmartWizardIf you need to bypass validation during backward

Looking for assistance with Jquery smartwizard? Check out the link. I want to disable validation when a user clicks on the "Previous" button in any step, except for the first step where the Previous button is disabled by default. Below is my JavaScript co ...

Using Javascript to dynamically add SVGs from an array

Having issues with displaying SVG images in a quiz I'm building. I have a folder full of SVGs that correspond to each multiple choice option, but I can't seem to get the pathway right for them to show up properly. I've tried using template l ...

The issue with jqXHR.responseText property missing in IE versions lower than 10 is causing problems with jQuery File Upload response handling

Encountering issues with ie8/ie9 when attempting to receive response HTML from the server. $(function() { $('#fileupload').fileupload({ url: '@Url.Action("Save", "CounterDoc")', dataType: 'json', f ...

When attempting to import * as firebase from 'firebase/app', the result is null

Recently, I've been encountering an issue while attempting to incorporate firebase v9 into my Next.js project. Every time I import it, it returns as undefined... import * as firebase from 'firebase/app' export function getFirebaseApp() { ...

Difficulty sending a parameter to the onClick function of a React Button

I'm struggling with passing parameters to my callback function when clicking a material-ui button. Unfortunately, the following approach is not yielding the expected results. const fetchData = async (param) => { } <Button onClick={fetchData(&a ...

A TypeScript/Cypress command that restricts input parameters to specific, predefined values

I have a Cypress command with a parameter that I want to restrict so it only accepts certain values. For example, I want the columnValue to be limited to 'xxx', 'yyy', or 'zzz'. How can I achieve this? Cypress.Commands.add( ...

Firefox version 78.0.1's Responsive Design Mode fails to provide accurate window.innerWidth values after screen orientation change

Could this be a bug specific to Firefox, or have I made an error somewhere? When testing in Chrome or on an actual device like Android, everything works fine. It appears to be an issue only when using Firefox's Responsive Design Mode. Below is the c ...

Conceal the heading 4 element when a specific text is searched

I have a search script that filters the text, but I want to hide the h4 element at the top of each ol. How can I achieve this? The issue I'm facing is that while the text gets filtered correctly, the h4 element (which should be hidden) remains visibl ...