最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use moment in nestjs application - Stack Overflow

programmeradmin5浏览0评论

I want to use momentjs in a nestjs app, and also be able to test my services. So I provided momentjs as below in my module

providers: [
    {
      provide: 'MomentWrapper',
      useFactory: async () => moment(),
      scope: Scope.REQUEST,
    },
  ],

and in my service

    constructor(
        @Inject('MomentWrapper') private momentWrapper: moment.Moment
    )

Then I have a method like this

    private calculateNextRun(every: number, period: SchedulePeriod): string {
        const currentDate = this.momentWrapper.tz('America/Toronto');
        let nextDate = null;

        nextDate = currentDate.add(every, period);
        console.log(currentDate.format(), nextDate.format(), every, period, );

        return nextDate.toISOString();
    }

This method will be called in a loop, and supposed to get the current date and add some days/weeks/.. to it and return it.

The issue is it keeps the old value so each time it goes into the method is not starting from current date

console output

2020-01-25T16:39:19-05:00 2020-01-25T16:39:19-05:00 6 d
2020-02-15T16:39:19-05:00 2020-02-15T16:39:19-05:00 3 w
2020-02-16T16:39:19-05:00 2020-02-16T16:39:19-05:00 1 d

If you see first date, which is currentDate keep changing

Is there any way to overe this issue, without creating a new service like this

import * as moment from 'moment-timezone';
@Injectable()
export class MomentService {
    moment(): moment.Moment {
        return moment();
    }
}

I want to use momentjs in a nestjs app, and also be able to test my services. So I provided momentjs as below in my module

providers: [
    {
      provide: 'MomentWrapper',
      useFactory: async () => moment(),
      scope: Scope.REQUEST,
    },
  ],

and in my service

    constructor(
        @Inject('MomentWrapper') private momentWrapper: moment.Moment
    )

Then I have a method like this

    private calculateNextRun(every: number, period: SchedulePeriod): string {
        const currentDate = this.momentWrapper.tz('America/Toronto');
        let nextDate = null;

        nextDate = currentDate.add(every, period);
        console.log(currentDate.format(), nextDate.format(), every, period, );

        return nextDate.toISOString();
    }

This method will be called in a loop, and supposed to get the current date and add some days/weeks/.. to it and return it.

The issue is it keeps the old value so each time it goes into the method is not starting from current date

console output

2020-01-25T16:39:19-05:00 2020-01-25T16:39:19-05:00 6 d
2020-02-15T16:39:19-05:00 2020-02-15T16:39:19-05:00 3 w
2020-02-16T16:39:19-05:00 2020-02-16T16:39:19-05:00 1 d

If you see first date, which is currentDate keep changing

Is there any way to overe this issue, without creating a new service like this

import * as moment from 'moment-timezone';
@Injectable()
export class MomentService {
    moment(): moment.Moment {
        return moment();
    }
}

Share Improve this question edited Jan 21, 2020 at 8:21 Kim Kern 60.5k20 gold badges216 silver badges213 bronze badges asked Jan 19, 2020 at 21:51 RezaReza 19.9k16 gold badges98 silver badges173 bronze badges 4
  • Does it work when you define your provider as useValue: moment and then call this.momentWrapper().tz(...? – Kim Kern Commented Jan 19, 2020 at 23:13
  • @KimKern I changed it to what you asked, anyway I get syntax error for this.momentWrapper() , BUT if I use my old syntax with your changes, seems it's working – Reza Commented Jan 21, 2020 at 1:00
  • 1 @KimKern please post it as answer to accept – Reza Commented Jan 21, 2020 at 1:05
  • This is a typescript issue importing the package see the answer below stackoverflow./a/71943285/8255981 – Miebaka Commented Jul 27, 2023 at 14:40
Add a ment  | 

2 Answers 2

Reset to default 8

This works fine for me:

// in some *.service.ts
import * as moment from 'moment';


{ userIsRegistredOn: moment()}

You should create a new moment instance for each function call and not reuse the same instance of your provider singleton. So provide moment instead of moment():

providers: [
    {
      provide: 'MomentWrapper',
      useValue: moment
    },
  ],
发布评论

评论列表(0)

  1. 暂无评论