I got a view in angularjs and I'm just trying to display the current date.
html
<input type="text" class="form-control" ng-model="lis.ModifiedDate" id="ModifiedDate">
app.js
$scope.ModifiedDate = $filter("ModifiedDate")(Date.now(), 'yyyy-MM-dd');
Can you please assist me here i want to display default date and the save it on a table.
change the controller as,
$scope.ModifiedDate = $filter("date")(Date.now(), 'yyyy-MM-dd');
dont forget to inject $filter in to the controller
and html, you need to bind the value to textbox using ModifiedDate not with lis.ModifiedDate.
<input type="text" class="form-control" ng-model="ModifiedDate" id="ModifiedDate">
here is a sample Plunker
Ok. What you're asking for is how to format the data from the ng-bind and also edit it. What you need to do is little known and called a formatter:
How to do two-way filtering in angular.js?
You need to define functions that both read and write the formatted date. That is actually easy, but Date() parses date formats pretty well. You cannot just use filter to do this though.
Alternatively, if you're happy with a long-date format in the field you CAN edit that, and it will be updated as a normal bound value. Pretty delicate though - see plunkr:
http://plnkr.co/edit/MPfGoNsG74rAV6UuvBKk?p=preview
<body ng-controller="MainCtrl">
<p>Edit the date in the input:</p>
<input ng-model="boundDate">
{{boundDate | date : 'yyyy-mm-dd'}}
</body>
app.controller('MainCtrl', function($scope, $filter) {
$scope.boundDate = new Date();
});
Use moment js it can convert date as required format
$scope.formattedDate = moment(new Date()).format("YYYY-MM-DD");
In html you use directly {{formattedDate}} it displays date in yyyy-mm-dd format
Related
I am using PrimeNG Calendar control I am able to select and store date but when I fetch dates from say database and trying to set it by updating Modal. I am getting the following error :
<p-calendar [(ngModel)]="bin.bidinstalledfrom" dateFormat="dd/mm/yy"
[showTime]="true" autocomplete="off" required name="BIDInstalledFrom"
id="installedfrom" class="col-md-12" ></p-calendar>
Component:
this.bin.BIDInstalledFrom='17/05/2018'
;// I have hardcoded the date in component for the ease of development
I have tried dates in various formats. '17/05/2018 16:00', '17/05/2018','1526572848' etc
I have also tried changing the datatype of modal property bidinstalledfrom to string,date,number. Same issue. And since I am getting error, not only am I not able to set the date but also calendar doesn't work.
What am I missing here?
Primeng Calendar accepts Date object. So you have to convert your date string to JS date object. Eg:
new Date('17/05/2018');
And then assign it to your model.
For PrimeNG Calendar you need to use the JavaScript Date object and do the instantiation correctly. For instance with new Date('mm/dd/yyyy') (see https://www.w3schools.com/js/js_dates.asp and https://www.w3schools.com/js/js_date_formats.asp).
Template:
<p-calendar [(ngModel)]="bin.bidinstalledfrom"
[showTime]="true"
dateFormat="dd/mm/yy">
</p-calendar>
Component:
this.bin.bidinstalledfrom = new Date('05/17/2018');
You can find a working demo here: https://stackblitz.com/edit/prime-ng-calendar-38du98
I build android app and in my app have some search function with input type date. I try used parse input date from 2017-5-10T17:00:00.000Z to 2017-5-10 with moment.angular. in search function work but it make my app error and wan't move to anothe page just stact in search page. I don't know where is error
this my html
<label class="item item-input">
<span class="input-label">Start</span>
<input type="date" ng-model="trip.start">
</label>
and this my controller
$scope.trip.start = moment($scope.trip.start).format("YYYY-MM-DD");
and i add some srcin my index
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
please help me solve this problem
thanks
moment($scope.trip.start).format("YYYY-MM-DD") is not in Angular date object format.
As a work around you can use moment($scope.trip.start)._d.
So your code would be
$scope.trip.start = moment($scope.trip.start)._d;
the issue is mostly with date format. The date given is 2017-5-10T17:00:00.000Z which is of format YYYY-M-DD and not a standard ISO format, hence moment is mostly not able to parse it. As per ISO the date should be 2017-05-10T17:00:00.000Z
Note: .format has nothing to do with input format, its for output.
Note: Cant comments hence adding this to answer: the order of JS is correct. moment is independent of angular hence it does not need to appear after angular.
EDIT
You can try below to get the date
moment(Date.parse($scope.trip.start)).format("YYYY-MM-DD");
Sorry about the bad title, I didn't exactly know how to phrase that any better. I'm going to try to explain what I need as best as possible.
I'm currently making a Task Manager (a ToDo list) in Javascript where you can create tasks, set a schedule for them and then you would have a list with all the tasks that you need to do and the respective schedule of each task (beginning and ending date).
I'm using Angular.js and Angular Material to do my UI. I have the Angular Material's datepicker. This is the code I use to get the date value:
angular.module('KBTM_App', ['ngMaterial']).controller('AppCtrl', function($scope) {
$scope.myDate1 = new Date();
...
// This is where I get the date value
var initial_date_1 = angular.element(document.querySelector('[ng-controller="AppCtrl"]')).scope().myDate1;
The problem is that this datepicker is returning the date in this format:"2016-04-29T08:36:10.027Z", which I don't want. I want a more normal format, like "DD-MM-YYYY".
I tried converting the date format with Moment.js, but that didn't work. Here's the code I used for that:
var initial_date = moment(initial_date_1, "YYYY-MM-DD'T'HH:MM:SS.SSS'Z'").format("DD-MM-YYYY");
And then I would store that date in the localStorage:
// Get the other dates already in the list
var initial_dates = get_initial_dates();
// Adds the new date to that list
initial_dates.push(initial_date);
// Stores those dates in the localStorage
localStorage.setItem('initial_date_db', JSON.stringify(initial_dates));
With that code I get "Invalid date" when trying to add a new task to the task list.
Sorry for the long post!
Thanks to whoever can help me.
You can convert the date format in html using angular material datepicker by using following code.
data-ng-value="myDate1 | date : 'shortDate'"
Use the angular date filter to format the date. Refer the link angular date
var initial_date = $filter('date')($scope.myDate1, "dd-MM-yyyy");
Inject the $filter in your controller main module.
Best way to handle dates is to use momentjs and there is a special AJS Directive available. angular-moment
You can check [momentjs][2] offical docs for detailed info. You have to add angular-moment as dependency injection to use moment easily. https://github.com/urish/angular-moment#instructions-for-using-moment-timezone-with-webpack
I'm using AngularJS to for user to choose week from a input week tag, but there is something bizarre qbout the format.
I've read that the format should be yyyy-W##, and I do this already, but angularJS still gives me this error:
Error: [ngModel:datefmt] Expected `2015-W51` to be a date
http://errors.angularjs.org/1.4.8/ngModel/datefmt?p0=2015-W51
and this is my tag:
<input type="week" ng-if="header.type==='week'" ng-model="entry[header.className]" ng-change="vos.fieldSelectionChanged(field_id,entry.record_id)"/>
So as you see, the input type is week, here, I'm using this tag inside of a ng-repeat, so I'm loading the data from the entry[] array, and the week loaded is 2015-W51.
So please tell me what I'm doing wrong , is there a best practice using this tag with AngularJS?
Thanks !
Edit 1 - more code
I've found this:
http://codepen.io/empirefox/pen/MYyoao
It demonstrates how to use input week with a date, but the problem is that I don't have the data as dates.
And at the right part of the page, you have this :
$scope.value = new Date(2013, 0, 3);
Change it to this :
$scope.value = "2015-W20";
And it gives me the same error. So if we can solve this one, I think we can also solve the problem on my page.
Angular uses native Date object to handle all its datetime-related inputs, but unfortunately Date instances does not have any methods to handle week numbers. It would be the best solution to stick with moment.js.
Coercion from string to ng-model-enabled value (in controller etc.):
$scope.value = moment("2015-W20").toDate();
Coercion from ng-model-enabled value to string (in form submit handler etc.):
moment($scope.value).format("YYYY-[W]WW"); // returns "2015-W20"
So I receive my app data via an API returning JSON data. With that, my time stamps are always returned with an undesirable look. Always like this:
2013-11-25T12:44:02
So I am using angular to build my app and couldn't see a way to format this with simply returning something like:
<div>
<p ng-repeat="date in ppt.Alerts | filter:{type: DenialStatement}"><span>{{date.date}}<span> <span>{{date.descr}}<span></p>
</div>
In this case, date is that time property. So I was figuring within my html, if I could benefit from using moment.js to format this or a better way to do this across the board.
Can anyone here assist with this?
Thanks much.
Your text is string representation of date object, you can assign
date.date = new Date(input)
during data retrieval this will work fine. And you can print directly angular built in date filter
<span>{{date.date|date :'dd/MM/yyyy'}}<span>
or any format you prefer.
Angular has built in date filter
{{date.date | date : format : timezone}}
See date filter docs for format and timezone usage