0

I have one function in which I calculate the time and date. It works fine in Chrome but does not work in my firefox.

Here is my code

 // check batch timing
  async check_batch_timing(batch) {
    try {
      const date_obj: any = await this.http
        .get(environment.time_api_url)
        .toPromise();

      const current_day_time = {
        current_time: new Date(
          `jan 0, ${new Date(date_obj.currentDateTime).toTimeString()}`
        ),
        current_day: date_obj.dayOfTheWeek,
        current_date: new Date(
          `${new Date(date_obj.currentDateTime).toDateString()},0:0:0`
        ),
      };

      batch.forEach((bat, index) => {
        console.log(bat.batch_ids.batch_end_time);
        const batch_info = {
          batch_start_date: new Date(
            `${new Date(bat.batch_ids.batch_start_date).toDateString()},0:0:0`
          ),
          batch_end_date: new Date(
            `${new Date(bat.batch_ids.batch_end_date).toDateString()},0:0:0`
          ),

          batch_start_time: new Date(
            `jan 0, ${new Date(bat.batch_ids.batch_start_time).toTimeString()}`
          ),

          batch_end_time: new Date(
            `jan 0,${new Date(bat.batch_ids.batch_end_time).toTimeString()}`
          ),
        };

        console.log(batch_info);

        if (!this.batch_timing) {
          if (
            moment(current_day_time.current_time).isBetween(
              batch_info.batch_start_time,
              batch_info.batch_end_time
            ) &&
            current_day_time.current_date >= batch_info.batch_start_date &&
            current_day_time.current_date <= batch_info.batch_end_date &&
            bat.batch_ids.active_days.includes(current_day_time.current_day)
          ) {
            this.active_batch = bat.batch_ids;
            this.batch_timing = true;
            this.get_chat_document();
          }
        } else {
          return;
        }
      });

      this.batch_timing ? '' : this.show_error();
    } catch (error) {
      this.spinner = false;
      this.error_handler(error);
    }
  }

output in Chrome

{ "batch_start_date": "2020-06-30T18:30:00.000Z", "batch_end_date": "2021-09-29T18:30:00.000Z", "batch_start_time": "2000-01-01T03:30:42.000Z", "batch_end_time": "2000-01-01T12:30:52.000Z" }

output in Firefox

{ "batch_start_date": "2020-06-30T18:30:00.000Z", "batch_end_date": "2021-09-29T18:30:00.000Z", "batch_start_time": invalid Date, "batch_end_time": invalid Date }

I don't have any idea what's going on.

Kindly Help

Mohd Sabban
  • 182
  • 1
  • 12
  • That's because string parsing works differently in each browser. To make it work consistently you should either parse it itself and provide to constructor year, month, day, time or use some library like moment.js – vitaliy kotov Aug 01 '21 at 10:42
  • None of the formats you're parsing with the built–in parser are supported by ECMA-262 so parsing is implementation dependent. Also, consider the inefficiency of parsing a string to Date, producing a string, then modifying the string to parse it again into another date. Far better to parse the string once, it's maybe two lines of code. – RobG Aug 01 '21 at 22:26

0 Answers0