DateFormat in angularjs - angularjs

I wanted to achieve this format: "Saturday, August 30, 2014"
I have this code in my view:
{{masterlist.created_date}}
And the result looks like this:
/DATE(1452842730000)/
What should I do in my controller or in the view? Thanks.

First of all you date should be in correct format
Use datefilter from angular
{{masterlist.created_date | date:'fullDate'}}
Here is the doc

Angular provides many filters, among them one is for date. It provides various format to format your date. Read about them here: date filter: angularjs
But filters can prove performance bottleneck, so its better to avoid them nd format your date on server side.

Related

MomentJs/Angular Date filter not working in IE

I've my date format as '2017-05-16T13:22:01.207Z' and trying to convert to '05/16/17 08:22 AM'. I've tried the following and none of them worked for me in IE, works fine in chrome. Any ideas what is the reason. Did anyone ever face this situation.
Date = '2017-05-16T13:22:01.207Z';
MomentJs Solution:
$moment(Date).format('MM-DD-YY HH:mm A').replace(/-/g,"/")
AngularJs Date Filter:
{{Date | date: 'short'}}
Expected Result: I'm seeing this in chrome, but not in IE
05-16-17 13:22 PM
Hehe, the replace might be the funniest way I've seen someone handling the format() function, which already formats a date as one wishes.
Use
var date = '2017-05-16T13:22:01.207Z';
alert(moment(date).format('MM/DD/YY HH:mm A'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
Also note!!!
Date is already defined in js (it's the global Date object); so you'd be better off using some different name for your variable - like the similar sounding date; but with a lowercased D/d. (This might be where your error comes from)

How to format dates from server into JS

It might be a super simple solution, but I am struglling with it for a while now.
I am using angular.js and using input time.
The server returns the following date convention:
shiftStarted = "2017-01-17 19:55:11"
I want to put it as ng-model in the input field:
<input type="time" ng-model="shiftStarted" />
The problem is that input time should receive a valid JS date format,
In order to try to solve it I used this solution I found on the web:
shiftStarted = new Date(shiftStarted.replace(' ', 'T'));
But the following is changing the timezone to UTC.
How can I simply put the date accurately in the input time field without handling tough timezone issues?
Best,
Omri
If your date has been formatted as YYYY-MM-DD, the browser will parse it as an ISO date. (the full format would be 2017-01-17T18:03:26+00:00 at the time of me writing this)
Since no timezone portion was provided (only a date), the browser assumes the timezone is UTC by default.
If you just want to parse a date without bothering with timezones, you could use MomentJS (as suggested by Ananth)
Code would be:
shiftStarted = moment(shiftStarted, "YYYY-MM-DD").toDate();
There is the angular-date directive:
<input type="text" datetime="yyyy-MM-dd HH:mm:ss" ng-model="shiftStarted">
This also include a parser service. New timezone additions too.
https://github.com/eight04/angular-datetime
You could try date.getTimezoneOffset()

AngularJS Date filter pass i18n locale for single invocation

now i use "date" filter
{{ controller.day | date : 'EEEE - MMM d yyyy' }}
can i add something like 'locale'
{{ controller.day | date : 'EEEE - MMM d yyyy' : 'it-IT' }}
for locale the date in italian, but the rest of application remains in english?
Yes, you can.
...convert the date in javascript
var convertedDateString = dateToConvert.toLocaleString();
var convertedDate = new Date(convertedDateString);
... later, in angular
{{convertedDate | date:'medium'}}
I don't quite know where you're going (what's the point), but there are two ways to achieve your goal.
You can use Angular Dynamic Locale to load Angular L10n files dynamically. There is even a valid example how to set your target locale on it's GitHub page: tmhDynamicLocale.set('it');
You can create your own filter with arbitrary formatting. For instance you can use moment.js to format dates.
Personally, I would recommend first path. This will also format numbers according to a locale.
I definitely not recommend using vanilla JavaScript's Date.toLocaleString();. This function should always be given a locale as a parameter: Date.toLocaleString(currentLocale);. The only issue with the latter is support for ECMA-402 standard, which is implemented by modern web browsers only. Should you need to support ancient web browsers (such as IE up to v. 10), you cannot expect it to work.

how to get calendar format date in AngularJs

I have a field "timeStamp:2015-12-08T11:58:46.650-06:00" using this I have to display it has Today and also Tue 11:58AM. How to do this in AngularJs
You should be able to use the built in date filter. So you can display it like this:
{{myDate | date : mediumDate}}
In this case 'mediumDate' is one of the built in formats available.
Check out the various formats here.
https://docs.angularjs.org/api/ng/filter/date
You can do some simple format with the date filter, but for advanced things, like displaying the date/time relative to a concrete moment, you should try MomentJS, wich can handle that and much more.
The functions you're looking for are documented here: http://momentjs.com/docs/#/displaying/fromnow/
It should be easy to set a custom filter with that. Something like this:
app.filter('moment', function() {
return function(dateString) {
return moment(dateString).fromNow();
};
});

Convert datetime in decimal trough angularjs

I would like to ask you, how can I convert decimal number to data in angularJS?
source:
20150911141804
expected:
2015-09-11
I need format this source number inside cellTemplate in ui-grid.
Thanks
If you need to display something you're getting from a data source, like a JSON file or a $http.get(...), then using a filter is the way to go, but I suggest you build a custom one, seeing your format is quite uncommon.
If you need to parse a value entered by a user in, say, an input text, then the right way is to use a custom parser.
If you want to know more you can read the official docs for filters and parsers.
use
<span>{{1288323623006 | date:'yyyy-MM-dd'}}</span><br>
refer to
https://docs.angularjs.org/api/ng/filter/date
Your date has a very strange format.
If you cannot change it (to an UNIX timestamp for example), consider converting your date (as a String) to something like:
20150911141804 => '2015-09-11'
And then create a new date from this String :
var date = new Date('2015-09-11');
Once you have a real Date object, you can format it using angular's native date filter or momentjs.

Resources