How can I show the unique phrase "Today" on a specific date (date=today) with the Angular-UI-Bootstrap Datepicker?

I'm currently integrating the Angular-UI datepicker directive into my project and I'm facing an issue. I want to have the datepicker display 'Today' instead of the actual date like 2015/04/27.

Is there a way to achieve this without having to change the $scope.dt to "Today" and then deal with converting it back when saving to the database? It seems like it might require a custom format, but I can't figure out how to implement that based on the existing code.

Answer №1

If you want to create a filter in AngularJS that can customize the date format, you can wrap the original date filter and handle the formatting based on certain conditions. Here's a basic outline of how you can achieve this:

.filter('my_date', ['dateFilter', function (dateFilter) {
    function filter(date, format, timezone) {
        var today  = new Date();
        if (!(date instanceof Date)) {
            date = new Date(date);
        }

        if (format == "today") {
            if (date.getFullYear() == today.getFullYear() && date.getMonth() == today.getMonth() && date.getDate() == date.getDate()) {
                return "Today";
            } else {
                format = "yyyy/mm/dd"; // default format
            }
        }

        return dateFilter(date, format, timezone);
    }
    return filter;
}])

To use this filter, you can simply do:

{{dt | my_date:'today'}}

There are potential improvements that can be made to this filter, such as handling different input formats and making the default format customizable.

NOTE: This technique is suitable if you have control over how dates are displayed in your application. If you are using AngularUI's datepicker-popup, which uses the built-in date filter, you may need to implement your own logic for date formatting.

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

using javascript to target a specific css selector attribute

I have a CSS file with the following code: .datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4; font-size: 16px ;font-weight: normal; } Is there a way to use JavaScript to dynamically change the font size? The code below worked ...

Error: Unable to call the 'model' function on the 'users' object in Mongoose due

I'm attempting to implement Mongoose's method for defining models in a separate document from their schemas, especially when dealing with multiple databases. Here's the example provided by Mongoose's Docs for User.js: const userSche ...

Error: An unexpected identifier was encountered while executing my function

I've been trying to implement a function that I found online, but when I try to run it in the terminal, I keep getting this error: /home/simone/gekko/strategies/high.js:10 sma: function(name, price, points) ^^^ SyntaxError: Unexpected identifier I ...

Display the username on a Meteor React component

I'm facing an issue with one of my React components, which serves as the main layout for my application. It includes a navigation bar that displays the username of the currently logged-in user. The problem arises when Meteor.user() returns undefined d ...

What content appears post-iframe?

After researching for a while, I've only come across information about text displayed after a broken iframe on this topic. However, what I actually want to do is display text below an iframe set at 90% height, like so: https://i.sstatic.net/KHXI3.png ...

The functionality of the button for selecting and deselecting all checkboxes is currently malfunctioning

I have a button in my ng-grid that successfully selects all checkboxes. However, I am also trying to implement functionality to deselect the checkboxes using the same button. Here is the initial code: app.directive('selectAll',function(){ ret ...

What could be the reason for the misalignment between md-menu and md-button when ng-href is included in the button?

Check out the demo by clicking here: http://codepen.io/audiodude/pen/vLyNvw The main distinction between the top and bottom sections lies in the fact that the top section utilizes an md-button with an ng-href attribute, which Angular Material compiles int ...

Avoid displaying "undefined" on HTML page when Firebase database value is empty

I am trying my best to explain my issue, although I struggle with scripts. Please bear with me and help me improve. ˆˆ Below is the script I am working on. The goal is to display all records with a date set for 'today or in the future'. Progr ...

Showing recurring HTML elements with conditional content

I am displaying multiple bootstrap badges in a table column with different colors based on the values. Each badge has a unique class and style name to differentiate the colors. I feel like there is a lot of repetition in my HTML code. Is there a way for me ...

I encountered a Vue warning indicating an issue in the v-on handler: "Error: Request failed with status code 404"

I recently started learning Vue.js and attempted to follow a tutorial on integrating Axios. Unfortunately, I keep encountering an error that I can't seem to resolve. I initially ran it on localhost:3000, then switched back to 8080, but the issue persi ...

Having trouble establishing a default route with React Router v5

I am facing an issue with setting the default route to the home page in react router v5. Despite trying several methods, I cannot get it to work as expected. Index.js import React from "react"; import ReactDOM from "react-dom"; import ...

Display a division upon choosing an option

I am working on a project that involves a selection menu in the form of a drop-down list. <select> <option id="one" value="something">Car</option> <option id="two" value="anything">Plane</option> </select> Also, I ...

Exploring the World of Infinite Scrolling with React.js and Material_ui

I am currently working on a project using react.js As part of this project, I need to implement a board with infinite scroll similar to Facebook I have a specific question regarding this implementation. When scrolling the board and loading more posts li ...

The mobile menu is not responding to the click event

Upon clicking the mobile menu hamburger button, I am experiencing a lack of response. I expected the hamburger menu to transition and display the mobile menu, but it seems that neither action is being triggered. Even though I can confirm that my javascrip ...

Filtering with AngularJS based on an array of IDs

span class="list-group-item" ng-click="filterByCtg = {ctgid:'12'}">category div ng-repeat="product in products | filter:search | filter:filterByCtg |orderBy:orderByFilter:reverse"> I need to assign an array of IDs to the filterByCtg for ...

Passing values dynamically to a JavaScript function within a Chrome extension using Python at runtime

I have encountered a unique challenge in my automation work, which I detailed in this query It's important to note that I am utilizing Python with Selenium WebDriver As I navigate through a series of changes and obstacles, I find myself exploring th ...

Create a new function within the GraphQL Resolvers file

I am trying to define a function within the same ts file as where I specify the resolvers export const resolvers = { Query: { books: () => { return [ { title: 'Harry Potter and the Chambe ...

Node.JS executes Sandbox within a RESTful service environment

Utilizing the Node Restify Module to develop a REST service that accepts POST requests. Inside the service, I am attempting to create a Sandboxed process using the Node Sandbox module in order to execute dynamically inserted JavaScript without impacting th ...

Import Socket.io into your Node.js application using the import statement

Can't seem to figure out why I keep encountering this error. Everything works perfectly when I use the request method instead. import express from 'express'; import { createServer } from 'http'; import * as io from 'socket.io& ...

Identify unique special characters without the need for a specific key code

When you press the backspace key, the console may display an empty string for keyVal, which can be misleading because even though it appears empty, keyVal.length is actually equal to 1 due to a hidden character. element.on('keydown',function(e){ ...