Ways to automatically display the date upon loading the view

I have integrated a plugin with Angular, which is essentially an extension. Upon loading the view, only an empty input is visible. Clicking on the input reveals a calendar with today's date displayed.

My objective is to automatically load today's date in the input when the view is loaded.

This is how I am currently using it:

<input type='text' datetimepicker
       datetimepicker-options="{format: 'MM/DD/YYYY', useCurrent: true}"/>

Any suggestions on how to achieve this?

EDIT

I tried adding autofocus to the input, but it causes the datepicker to show up immediately, whereas I need just the date to be displayed.

MY CONTROLLER:

(function () {
  'use strict';

  angular
    .module('palpatine')
    .controller('RotationsCtrl', RotationsCtrl);

  /* @ngInject */
  function RotationsCtrl (Rotations, $scope, $rootScope, $state) {
    /*jshint validthis: true */
    var vm = this;

    activate();

    function activate () {

      $(document).ready(function () {

        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();

        if(dd<10) {
            dd='0'+dd
        }

        if(mm<10) {
            mm='0'+mm
        }

        today = mm+'/'+dd+'/'+yyyy;

        $scope.today = moment().format("MM/DD/YYYY");

        console.log(today);

        $scope.datetimepickerOptions = {
          format: 'MM/DD/YYYY',
          defaultDate: today
        };


      });

    }
  }
})();

Answer №1

To ensure the current date is set as default, simply assign it to defaultDate. This action will take precedence over the use of useCurrent, rendering it unnecessary.

Within the controller:

$scope.datetimepickerOptions = {
    format: 'MM/DD/YYYY', 
    defaultDate: new Date()
};

HTML markup:

<input type='text' 
       datetimepicker
       datetimepicker-options="{{datetimepickerOptions}}"/>

Update:

The issue was identified in the 0.1.3 release of the diosney/angular-bootstrap-datetimepicker-directive where a bug with defaultDate existed. The resolution involved updating to the master branch.

bower.json

"angular-bootstrap-datetimepicker-directive": "#master"

Then execute the following command:

bower update

Answer №2

Focus on the text box and then remove focus...

 $('.input_focus').trigger('focus');
 $('.input_focus').trigger('blur');

Alternatively,

 document.querySelector('#input_id').focus();
 document.querySelector('#input_id').blur();

Answer №3

Give this code a shot:

reference link

<input type='text' 
       datetimepicker
       datetimepicker-options="{format: 'MM/DD/YYYY', defaultDate: '01/01/2016'}"/>

Update

After reviewing the question, I noticed that the datetimepicker module is not loaded in the current module. Here's how you can inject the module:

angular
  .module('palpatine', [
    // Other modules injected.

    'datetimepicker'
  ]);

Answer №4

In need of a temporary placeholder for the current date? Simply add the placeholder attribute to your input and set today as the value.

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

In Node.js, while running unit tests, the importing function is limited to read-only access

Having trouble mocking an async function in Jest? I followed the documentation and used mockResolvedValue, but encountered a read-only issue when trying to import my mock function from another file. Check out my code below: //index.js async function get ...

Encountering an issue while attempting to send an image through JavaScript, jQuery, and PHP

I'm currently attempting to upload an image using JavaScript/jQuery, but I am unsure of how to retrieve the image in order to send it to a server (PHP). I have a form containing all the necessary information that I want to save in MySQL, and I use jQu ...

Mysterious occurrences always seem to unfold whenever I implement passport for user authentication in my Node.js and Express applications

At first, I wrote the following code snippet to define LocalStrategy: passport.use( 'local-login', new LocalStrategy({ usernameField:'username', passwordField: 'password', passReqtoCallback: tr ...

MYSQL table successfully created, however encountering a 500 error when attempting to post data

I've been working with the Node module KNEX for making MYSQL calls, and I'm trying to allow users to add their own custom tables. I have successfully added a database to MYSQL with all the necessary columns, but unfortunately, I keep encountering ...

Audio issues plaguing audio playback on Discord

After spending two exhausting days, I am still trying to figure out what is going on. I am currently working on a Bot for my Discord Channel that should play an audio.mp3 file when a specific command, like !laugh, is entered. However, despite trying variou ...

Tips for opening a variety of links in new tabs with unique popup buttons

Currently, I am facing a challenge where I need to use a button to trigger an ajax HTML popup and also navigate to a link simultaneously. The issue is that there are multiple buttons on the page, each needing to open different links. Any assistance would b ...

The command 'node' is not identified as an internal or external command, executable program, or batch file

While running "npm install node-sass" from git-bash-cli in Windows 10, I encountered a "'node' is not recognized as an internal or external command, operable program or batch file." error. This setup worked fine for years until I upgraded Node t ...

Adding input values to a jQuery Ajax POST request

I am currently attempting to send form values to a remote API using AJAX. The necessary information I require from the HTML form element includes the author, string, false, and true. At the moment, I have hard-coded some values but... function sendData ...

Sending a function as a callback to children components in order to update a specific child component

I am currently working on developing a Navbar component that undergoes slight changes when a user logs in through a SignIn component. Here is an overview of how my application is structured: Initially, I have defined a state in the App component where aut ...

What is the best method to redirect users who are not logged in from every URL when using PassportJs?

I am currently developing a web application using NodeJS, and I have integrated PassportJS with passport-local-mongoose for authentication. Below is the code snippet I've created for the root route to verify if the user is logged in: app.get('/ ...

What steps should I follow to properly set up my tsconfig.json in order to ensure that only the essential files are included when executing npm run build

Introduction I am seeking guidance on how to correctly set up my tsconfig.json file to ensure only the necessary files are included when running npm run build for my projects. I want to avoid any unnecessary files being imported. Query What steps should ...

Using Angular 4: Redirecting Menu to Component with Electron

I am currently working on my first project using Angular 4 and Electron to develop a desktop application. One of the challenges I'm facing is figuring out how to redirect to a specific component when a submenu item is clicked after overriding the ele ...

Journeying through a grid in AngularJS

My current project involves displaying a table where users can click on specific cells to highlight them. Now, I want to take it a step further and enable navigation of the highlighted cells using arrow keys on the keyboard. For example, if the user press ...

Encountered an issue while attempting to integrate Nebular into my Angular application

As a newcomer to Angular, I decided to try installing Nebular using the command ng add @nebular/theme. However, I encountered an error in the process. Upon entering the command into my terminal, the following error message appeared: ? Which Nebular theme ...

Incorporating a JavaScript advertisement zone within a PHP function

Currently in the PHP template, I am trying to embed a JavaScript ad zone inside a function in order to have control over each page's ad placement. Here is what I have: <?php if(is_page('welcome-president')) { oiopub_b ...

Utilizing prerender.io with lazy loading in Angular 2: A comprehensive guide

As Angular Universal is not expected to be included in the CLI for some time, I've had to resort to using prerender.io in order to ensure proper SEO functionality. However, my tests have shown that there are issues with lazy loaded modules causing SEO ...

Efficiently accessing and displaying nested models in AngularJS

Currently, I am in the process of developing a website that involves numerous relational links between data. For instance, users have the ability to create bookings, which will include a booker and a bookee, as well as an array of messages that can be asso ...

What would be the counterpart of setLocale in Yup for the zod library?

How can I set default custom error messages for Zod validation? If I want to use i18n for error messages in Yup, I would do the following: import { t } from "i18next"; import * as yup from "yup"; import "./i18next"; yup.setL ...

Solving the issue of image paths within external SCSS files in Nuxt.js

Trying to organize my component scss files separately from their Vue components has been a bit challenging. I also have a GLOBAL scss file that is not scoped, but no matter which approach I take, I can't seem to get the image paths in /assets or /stat ...

The functionality ceases to operate properly once a new element has been added

I am encountering an issue with the "click" action on a newly created element through a jQuery function, as it is not functioning properly. To demonstrate this problem, I have set up a JSFiddle at the following link (JSFiddle), however, please note that t ...