3

In my Firebase I have:

Firebase generated date for a created day.

Terms set by the user when the above was generated. 

So for example I have:

1439612582756 // formats to: Aug 14, 2015

15 // Formats to: "Net 15" in my code 

My code looks like this:

<td>{{invoice.settings.created | date}}</td>

<td>{{invoice.settings.created + invoice.settings.terms | date}}</td>

I have installed moment.js and 'angular-moment.js' to be able to format and play with dates, but I didn't see a way to add time to a date. This is for a for-each so I don't want to have to do any pre-scripting for this. Maybe a custom filter to add the terms to the date?

Obviously, my code above does not function. I just made that to demonstrate what I am wanting.

Any help would be appreciated!

UPDATE

Using this page: http://momentjs.com/docs/#/durations/add/ I came up with this filter:

.filter('dateTerms', function() {
    return function(created, terms) {
        var a = moment.duration(created, 'd');
        var b = moment.duration(terms, 'd');
        return a.add(b).days();
    }
})

But when I call it, I always end up with just 0?

<td>{{invoice.settings.created | date}}</td>
<td>{{invoice.settings.terms | dateTerms}}</td> // This line

I'm guessing it's because I'm only passing the filter the terms and not the created date. How would you pass two variables? I think I'm getting closer....

UPDATE 2

I dumped the moments crap. This is what I have now:

.filter('dateTerms', function() {
    return function(input, created) {
        var date = new Date(created*1000);
        date.setDate(date.getDate() + input);
        return (date.getMonth()+1)+'/'+ date.getDate() +'/'+date.getFullYear();
    }
})

And then in my html:

<td>{{invoice.settings.terms | dateTerms:1439746291480}}</td>

1439746291480 converts to `Aug 16, 2015`

This results in:

8/24/47601
7
  • you have a few things wrong here; specifically, created isn't a duration, it is a moment in time (fixed). That calculation won't give the results you expect. also, you aren't using the correct syntax for calling that filter. Commented Aug 16, 2015 at 17:57
  • K. Well, that didn't help at all. Commented Aug 16, 2015 at 17:58
  • @Claies Check update 2 on the question. Commented Aug 16, 2015 at 18:00
  • moment is much easier to use than what you are trying to do with that update; that filter could be a single line return moment(new Date(created)).add(terms, 'days'); Commented Aug 16, 2015 at 18:03
  • Okay, but all I want is 8/31/2015? That returns "2015-08-31T17:31:31.480Z" Commented Aug 16, 2015 at 18:04

1 Answer 1

4

Using method chaining with Moment.js, you can easily perform these kind of calculations in a single statement. For example:

return moment(new Date(created)).add(terms, 'days').format('MM/DD/YYYY');

Using Moment.js also will make your life easier in other ways, since it automatically deals with daylight savings, leap years, time zones, etc., making any of your date and time calculations more accurate.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.