how to calculate datedifference in angularjs? - angularjs

I am using angular js and trying to display a text ie 'recent' if the order was placed within 5 days ago from today. So if the difference in days between todaysDate and the Order.dateCreated <= 5 then I want to display 'recent' otherwise blank:
orderid | dateCreated | status
1 | 27-2-2014 | recent
2 | 27-1-2014 |
angular script fragment:
<table>
<tr data-ng-repeat="order in orders" >
<td>{{order.id}}</td>
<td>{{order.dateCreated}}</td>
<td><span ng-show="(getDate()-order.dateCreated)<=5">recent</span></td>
</tr>
</table>

You just need to do this in standard JavaScript and expose a method to your scope to allow the orders to use:
Something like this, however, if your dates are currently stored in a UK format as they appear to be in your question. You will not be able to parse them like this, consider having your server-side code spit out a standard format that JavaScript can parse.
If you find you are having problems doing that, cannot change the server-side response from that format, or want to keep the format, you could use a library to parse it, such as: http://momentjs.com/docs/#/parsing/string-format/ This also has some decent tools for checking the difference: http://momentjs.com/docs/#/displaying/difference/
$scope.isRecentOrder = function(date) {
// Assuming date is a string of parsable format: ie. "2014-01-27T01:00:00+00:00"
var diff = new Date() - new Date(date);
// Calculate from milliseconds
var days = ((((diff / 1000) / 60) / 60) / 24);
return days >= 5;
}
Then you can use the directive ng-show="isRecentOrder(order.dateCreated)"

Related

How to get raw date string from date picker?

I'm struggling for hours with this seemingly trivial issue.
I have a antd datepicker on my page.
Whenever I choose a date, instead of giving me the date I chose, it gives me a messy moment object, which I can't figure out how to read.
All I want is that when I choose "2020-01-18", it should give me precisely this string that the user chose, regardless of timezone, preferably in ISO format.
This is not a multi-national website. I just need a plain vanilla date so I can send it to the server, store in db, whatever.
Here are some of my trials, so far no luck:
var fltval = e;
if (isMoment(fltval)) {
var dat = fltval.toDate();
//dat.setUTCHours(0)
fltval = dat.toISOString(); // fltval.toISOString(false)
var a = dat.toUTCString();
//var b = dat.toLocaleString()
}
It keeps on moving with a few hours, probably to compensate for some timezone bias
UPDATE 1:
the datestring is data-wise correct. But its not ISO, so I cant use it correctly. I might try to parse this, but I cannot find a way to parse a string to date with a specific format.
UPDATE 2:
I also tried adding the bias manually, but for some reason the bias is 0
var dat = pickerval.toDate()
var bias = Date.prototype.getTimezoneOffset()// this is 0...
var bias2 = dat.getTimezoneOffset()// and this too is 0
var d2 = new Date(dat.getTime()+bias)
var mystring= dat.toISOString() //still wrong
Thanks!
Javascript date functions can be used,
I assume you are getting in 2022-01-03T11:19:07.946Z format then
date.toISOString().slice(0, 10)
to 2022-01-03
There are 2 ways to get the date string:
Use the moment.format api:
date.format("yyyy-MM-DD")
Use the date string that is passed to the onChange as second parameter
Here is a Link.
I am assuming your code snippet is inside the onChange method. This gives you a moment and a date string to work with (the first and second parameters of the function respectively).
You have a few options. You could set the format prop on the DatePicker to match the format of the string you want. Then just use the date string. Or you can use the moment object as Domino987 described.

Angular date filter: Format month as local time

I have a date formatted as a ISO-8601 string: overview.startTime = "2017-05-09T08:00:00Z"
I want to display this on my page and I have used the following code:
Dagens arbetspass {{overview.startTime | date:'dd-MMM'}}
This is displayed as "Dagens arbetspass 09-May". My problem is that this is a Swedish site, and in Sweden we don't start the month names with an uppercase character. Also May is written "maj" (j in the end and not y). I tried to add the timezone like this
Dagens arbetspass {{overview.startTime | date:'dd-MMM':'Europe/Stockholm'}}
but that did not change the output. In fact, most months are spelled differently in Swedish. Any suggestions?
Just use the javascript "toLocaleDateString" method to resolve the concern. The method takes two arguments which are 'locale' and 'options'.
Locale will be "sv-se" for swedish.
Options will provide the format to your string. For example -var options = { weekday: "long", year: "numeric", month:long",day:"numeric" };
var d = new Date("2017-05-09T08:00:00Z");
date.toLocaleDateString("sv-se", options)
Here's a plunker https://plnkr.co/edit/G2IL5Zv0OAcVRMZS9HB7

amTimeAgo in angular-moment make crazy calcs

So I'm trying to use directive amTimeAgo of momment JS in Angular, like this:
<span class="work-duration" ng-bind="item.startedAt | amTimeAgo: item.finishedAt: 'years' "></span>
I'm usting angular-moment (https://github.com/urish/angular-moment)
well, I want to show an date like, from: 02/09/2013 - to: 02/09/2015 - (2 years) or in different cases, 1 year and 3 months
Okay, in some cases the directive makes crazy calc,
some dates like this:
01/05/2012 - 30/09/2012 results in (3 years) why? Someone have any experiencie with this directive?
I tried to use the amFrom, but appears that this directive is not available in angularMoment,
in pure momment.JS:
var a = moment([2007, 0, 28]);
var b = moment([2007, 0, 29]);
a.from(b) // "a day ago"
Probably, this "from" method should works better than amTimeAgo.
Obs: in console, I got a lot of warnings about the finishedAt value:
angular-moment: Ignoring unsupported value for preprocess: 2015-10-01T03:00:00.000+0000

'date' filter without time zone in angularjs

This is input format:
yyyy:MM:dd'T'HH:mm:ss'Z' (Coming as a string from json service)
Required output format:
dd-mmm-yyyy
I have tried with {{txnDate | date:'dd-mm-yyyy'}}
but it is not working..
What is the format you are following for your date?
A quick var a = new Date(); a.toISOString(); in console will give you something like "2015-02-19T13:30:13.347Z". The formatted string you are receiving is not following any standard and I am afraid parsing it to date will result in Invalid Date in most of the browsers.
So you can either
Get your Date in proper format.
Make the best use of whatever is available. You can use split to break your string into individual components.
Something like:
var a = "yyyy:MM:dd'T'HH:mm:ss'Z'" //Replace with actual string
b=a.split(':') will result in ["yyyy", "MM", "dd'T'HH", "mm", "ss'Z'"] giving you year and months in b[0] and b[1].
For date, you can use b[2].substring(0,2) to give you dd.
You have all date components(apart from time components, which you don't need anyway) as string.
Either use them directly(as a string) or make a date object using these components(since you want month in MMM format).
$scope.txnDate = new Date(b[0]+'/'+b[1]+'/'+b[2].substring(0,2));
I am sure there are more ways to optimize this. Comment if this doesn't work for you, will try to elaborate more.

how to check datetime in angularjs?

posts = [{"content":"content1",
"created":"2013-12-27T14: 15: 27.747Z"},
{"content":"content2",
"created":"2013-12-27T14: 15: 02.956Z"}]
How to check posts[0] and posts[1] was created in the same day or not with Angularjs?
Angular doesn't really have a built in way to compare dates, but it can be done in conjunction with angular.
http://jsfiddle.net/TheSharpieOne/LxTGd/1/
This will compare if the day, month, and year are the same, determining if the 2 dates are on the same day (time is irrelevant).
$scope.sameDay = function(date1,date2){
return date1.substring(0,10) === date2.substring(0,10);
}
This functionality would ideally be made into some sort of directive. But this is just a basic example, a directive example can be made if required.
javascript cannot parse ISODATE which contain spaces:
post[0].created.replace(/\s/g,'')
That's how you compare dates:
(new Date(post[0].created).toDateString) === (new Date(post[1].created).toDateString)

Resources