Angularjs fullDate filter not working - javascript

I'm getting following error on using fullDate filter inside my html:
Error: [ngModel:nonassign] Expression 'publisherForm.dt |
date:'fullDate'' is non-assignable. Element:
Here is my jade code:
datepicker.well.well-sm(ng-model="publisherForm.dt", show-weeks="false", min-date="minDate")
input.form-control(type="text", ng-model=" publisherForm.dt | date:'fullDate' ", readonly='')
what I'm trying is show the selected date from ui-datepicker to user in another input field. Since I want to show only date, I'm using fullDate filter but in console I'm getting above mentioned error.
Any idea what can be the possible reason ?

On searching through existing threads on SO, I found following threads which helped me to solve my problem:
Using angularjs filter in input element
How to format a date using ng-model?
AngularJS get formatted date in ng-model
Finally made following lines of code change and it started working fine for me:
$scope.$watch('publisherForm.dt', function(newVal){
$scope.publisherForm.formattedDate = $filter('date')($scope.publisherForm.dt, 'fullDate');
});
If there is anyone who can suggest a better solution is always welcome.
Thanks

Related

How to insert Dates in mysql DB with mysql2? [duplicate]

After googling around, I cannot find a way to create a new table with a DATETIME column with the default format set to 'DD-MM-YYYY HH:MM:SS'
I saw a tutorial in which it was done in phpmyadmin so I suspect that I could use mysql via command line and achieve the same thing when creating my new table with
CREATE TABLE ()
Thank you in advance
"MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format."
This is from mysql site. You can store only this type, but you can use one of the many time format functions to change it, when you need to display it.
Mysql Time and Date functions
For example, one of those functions is the DATE_FORMAT, which can be used like so:
SELECT DATE_FORMAT(column_name, '%m/%d/%Y %H:%i') FROM tablename
Use DATE_FORMAT function to change the format.
SELECT DATE_FORMAT(CURDATE(), '%d/%m/%Y')
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
Refer DOC for more details
As others have explained that it is not possible, but here's alternative solution, it requires a little tuning, but it works like datetime column.
I started to think, how I could make formatting possible. I got an idea. What about making trigger for it? I mean, adding column with type char, and then updating that column using a MySQL trigger. And that worked! I made some research related to triggers, and finally come up with these queries:
CREATE TRIGGER timestampper BEFORE INSERT ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
CREATE TRIGGER timestampper BEFORE UPDATE ON table
FOR EACH
ROW SET NEW.timestamp = DATE_FORMAT(NOW(), '%d-%m-%Y %H:%i:%s');
You can't use TIMESTAMP or DATETIME as a column type, because these have their own format, and they update automatically.
So, here's your alternative timestamp or datetime alternative! Hope this helped, at least I'm glad that I got this working.
i have used following line of code & it works fine Thanks.... #Mithun Sasidharan
**
SELECT DATE_FORMAT(column_name, '%d/%m/%Y') FROM tablename
**
I'm pretty certain that you can't change the datetime format in mysql. The phpmyadmin setting is probably applying a custom format as it reads the datetime (using DATE_FORMAT or something from php). It shouldn't matter what format the database uses, format in the application to display it as you wish.
Date formatting is a pretty common task. I typically like to abstract it out into internationalization code or, if you don't need to deal with i18n, into a common date utility library. It helps keep things consistent and makes it easier to change later (or add i18n support).
No you can't; datetime will be stored in default format only while creating table and then you can change the display format in you select query the way you want using the Mysql Date Time Functions
This cannot be done for the table; besides, you even cannot change this default value at all.
The answer is a server variable datetime_format, it is unused.
Dim x as date
x = dr("appdate")
appdate = x.tostring("dd/MM/yyyy")
dr is the variable of datareader
try this:
DATE NOT NULL FORMAT 'YYYY-MM-DD'

Error in angular moment

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");

GroupBy Filter in AngularJS

I have an array of timeline which I want to divide in groups per date (dd/MM/YYYY). I was trying make my own "groupBy" without any library (which I really prefer), but I got no success. So I decided to search for a library that does it and I found this: angular-filter, so now I'm trying to use it.
The problem is that I can't figure out what I'm doing wrong, I followed this example and created my JS Bin to ilustrate my situation. It always says the format date is wrong, why?
By the way, the expected result is:
Date: 17/06/2016
Timeline item: an e-mail edited
Timeline item: an e-mail deleted
Timeline item: an e-mail edited
Date: 18/06/2016
Timeline item: an e-mail added
Timeline item: an e-mail added
Filters in Angular are called many times, not just once. So during the first digest, your created_at values are converted to D/M/Y format. Then, when during the next digest Angular call your filters again, it tries to create a date with already converted value (i.e. new Date('18/06/2016')). So you get an Invalid date as a result.
The solution is to convert the dates once in the controller before assigning them to the view and omit the map: ... filter.
See the updated jsbin.

How do I convert datepicker returned date format to a different format?

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

AngularJS/HTML input week format error

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"

Categories