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();
};
});
Related
I am fairly new to AngularJS and am having an issue with formatting the time object to populate my "time" input field in Android and Firefox. I have spent hours trying to figure out the issue and also utilized AngularJS's documentation to try and figure this out, but have had no luck.
I am dynamically populating an input field from JSON, and the result is always in the following format: 14:57:00.000
I need it to look like this in the input field: 2:57 PM
Here is the link to AngularJS that I have been referring to: AngularJS time input. At the bottom of the page is the plunker to run.
Thank you in advance for any assistance you can provide me.
you can use something like this
{{myTime | date: 'shortTime'}}
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)
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.
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.
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.