0
 @Cron('23 16 * * *')
  async transferData() {
    try {
      // Retrieve data from the AttendanceBulk table
      const attendanceBulkData = await this.AttendanceBulkModel.findAll();
  
      for (const data of attendanceBulkData) {
        const attendance = new Attendance();
        
        // Find the corresponding AttendanceAndBulk entry based on data.UserId
        const attendanceAndBulk = await AttendanceAndBulk.findOne({ where: { UserId: data.UserId } });
  
        if (attendanceAndBulk) {
          attendance.employeeId = attendanceAndBulk.employeeId;
          attendance.shiftType = data.Intime ? ShiftType.In : ShiftType.Out;
          attendance.time = new Date(`${data.Intime}`);;
          attendance.date = data.Date;
          attendance.outTime= data.Date;

          console.log(data.Intime)
          await attendance.save();
        }
      }
  
      this.logger.log('Data transfer completed successfully.');
    } catch (error) {
      this.logger.error('Data transfer failed:', error);
    }
  }


In above code I data.Intime is comming like 00:39:41 this. but attendance.date variable is Date type. how can I convert time varibale(00:39:41) to Date type in nest js?

Nadula
  • 37
  • 5

1 Answers1

1

You are only receiving hour/minutes/secondes

This is not sufficient to create a date

A solution that might work would be to create the date of the day, and then set the received time on this date

const [hours, minutes, seconds] = date.InTime.split(':');
const timestamp = (new Date()).setHours(hours, minutes, seconds);
const date = new Date(timestamp);

Here is the link of the documentation for the Date object, from wich we call the setHours methods https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours

If you plan on working with date a lot, i would also advise to checkout dayjs, a very convenient library for date manipulation in javascript https://day.js.org/