-1

I use loopback4. How do I make an expression to match datetime with time zone.I am interested by the hour parameter only: "finalhour" example: 2019-12-20T10:22:50.143Z ==> 2019-12-20Tfinalhour:22:50.143Z

I tried with this: const pattern=await '^'+"T"+finalhour+'^' but loopback usually read it as ^T10^

I'm resort to you after a long search in the forums.I will be thankful if you help me

H_Hmd
  • 25
  • 8

2 Answers2

0

From what i understand, you want to build a regex that match 2019-12-20TsomeNumber:22:50.143Z.

You could use this default ISO datetime regex

(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})\.\d{3,4}Z

And modify it to take your finalhour variable in it.

let finalHour = 10;
// when building regex from constructor, we need to espace the backslash ( \ )
// i did not know that.
let regex = new RegExp('(\\d{4})-(\\d{2})-(\\d{2})T' + finalHour + '\\:(\\d{2})\\:(\\d{2})\\.\\d{3,4}\\Z');

let test = '2019-12-20T10:22:50.143Z';

console.log(regex.test(test));

Also, you don't need an await keyword here because your are building a string, not waiting for a promise.

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • Hi Nicolas,I think I wrongly introduced my question, i add the whole method as response. Can you please help me – H_Hmd Jan 02 '20 at 08:40
0

I need to return the number of order per hour. so i try this solution but it return false comparison, for example if i have one order at hour:10 it return 6

 @get('/orders/count-perHour/{date}', {
    responses: {
      '200': {
        description: 'Order model count',
        content: { 'application/json': { schema: CountSchema } },
      },
    },
  })
  async countPerHour(
    @param.path.string('date') date: string): Promise<any> {
    let dateStart = new Date(date);
    dateStart.setSeconds(0);
    dateStart.setMinutes(0);

    let dateEnd = new Date(date);
    dateEnd.setSeconds(59);
    dateEnd.setMinutes(59);

    return await this.orderRepository.count(
      {
        createdAt: {
          lte: dateEnd
          ,
          gte: dateStart
        }
      }
    );
  }
H_Hmd
  • 25
  • 8