79369491

Date: 2025-01-19 18:07:29
Score: 1
Natty:
Report link

it is disabling the time slots but not according to what I want ,I wanted it to disable the time slots according to IST time zone but it was disabling according to UTC time , that was the problem I was facing but I got the answer to this , so I installed vercel in my vs code and deployed it to vercel through cli and I found the logs on the vercel project which i deployed through cli and the website was deployed on vercel only so I found that my server was in washington DC and when I was doing new Date() it was giving the local time zone of washington and not UTC time , as vercel and many other cloud platforms by default work in UTC only this thing I knew but it was not working and I had to explicitely mention .toIsoString() to get date in utc , also I did correction on the disabling dates logic ,earlier what I was doing was that I was creating a new Date by giving time and date to create a new Date so this was in utc and I thought it was in local time which was wrong on my side , I wanted some specific time of the selected date like 9:00 am, 10:00 am etc of ist and I was comparing it with current ist date ,if curr date time is greater than the time slots date and time like 9:00 or 10:00 etc then it would disable it but the mistake which I was making was that I was giving date and time to new Date () and thought it was local date and time but it was utc date and time and I was comparing utc time with curr Date because of which It was providing disabling according to utc time ,so what I did was first got the local date and specific time (9:00,10:00 etc) through datefns library I converted it into utc time using and then again converted that utc time to local time and then compared currDate with the formed date and if the currDate is greater than input date time then it will get disabled so now the logic is working correctly .

let {date}=req.params;
     console.log(date);
   
     const services = await Service.find({});
      const orders = await Order.find({date:date});
     
      let bookedTime = [];
      if(orders.length){
       bookedTime =orders.map((ord) => {
         return ord.time;
       });
      }
      
     const allTime = ["09:00","10:00","11:00","12:00","14:00","15:00","16:00","17:00","19:00","20:00"];
     let inputDateTime = [];

     const utcDate = new Date().toISOString();  // Current UTC time
     console.log("new Date() .to isostring : ",utcDate);
     const timeZone = 'Asia/Kolkata'
// Convert to IST (UTC + 5:30)
const istDate = dateFnsTz.toZonedTime(utcDate,timeZone);
console.log("backend ist date : ",istDate);
let currDate = istDate.getTime();

      for(let j=0;j<allTime.length;j++) {
        console.log(allTime[j]);
        let inputDateTimeString = `${date} ${allTime[j]}:00`;
    
// console.log(dateFnsTz);
// Convert to UTC
const utcDateTime = dateFnsTz.fromZonedTime(inputDateTimeString, timeZone);
       
        console.log("inputDate utc : ",utcDateTime);
        inputDateIST=dateFnsTz.toZonedTime(utcDateTime,timeZone);
        console.log( "input Date ist : ",inputDateIST);
    
        inputDateTime[j] = inputDateIST.getTime();
      }
      console.log('currDate in backend:',currDate);
console.log('inputDateTime in backend:', inputDateTime);

     res.render("orders/newTwo.ejs",{services,bookedTime,date,allTime,inputDateTime,currDate});
 

Note: the major reason behind this confusion , which I think was that when we run new date() in local environment and do console.log(date)then it prints date in utc and not localtime but internally it stores local time ,it is because of Node js which is running the javascript in our system it by default prints utc date even if the date stored is in localtime and on server side the new Date () by default gives utc date for many cloud platforms like vercel, it does it knowingly to standardise the server side configuration so that it is not dependent on server location, basically to standardise the process it uses utc by default on server side .

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Harsh