0

I have a unix timestamp property in my database propertyName is closeDeal.

closeDeal value in database - 1624699800 (unix timestamp). What I'm required to do is have two validations on the above timestamps

  1. the closeDeal timestamp is greater than the current timestamp
  2. the closeDeal timestamp should be >= currentDate_StartTime and <= currentDate_EndTime

Here the currentDate_StartTime would be the start time of the current date i.e (0,0,0,0)

Here the currentDate_EndTime would be the start time of the current date i.e (23, 59, 59, 999)

What I tried.

Below is my code of converting the currentDate to unixtimestamp and comparing.

Helper functions

getStartDateTimeStamp() {
        let currentTime = new Date();
        currentTime.setHours(0, 0, 0, 0);
        return Math.round((currentTime).getTime() / 1000);
    }

getEndDateTimeStamp() {
    let currentTime = new Date();
    currentTime.setHours(23, 59, 59, 999);
    return Math.round((currentTime).getTime() / 1000);
}

getCurrentDateTimeStamp() {
    let currentTime = new Date();
    return Math.round((currentTime).getTime() / 1000);
}

Date comparison in business logic

if(
   closeDeal > this.helper.getCurrentDateTimeStamp() // check if close deal is greater than current date
&& closeDeal >= this.helper.getStartDateTimeStamp() // closeDeal greater = than current Datetime
&& closeDeal <= this.helper.getEndDateTimeStamp() // closeDeal less = than current Datetime
 ) {
        //some logic here.

}

But the above code always fails

The JOKER
  • 453
  • 8
  • 21
  • do you get errors in the console? Did you check that those hlpers are getting referenced? The code looks fine – Kinglish Jun 25 '21 at 22:47
  • Can you show the values each function produces? The logic looks correct, unless there's possibly a timezone issue here. One small issue (certainly not the problem you're having) is that I'd do `new Date()` only once, so it doesn't have any chance of changing in value from one instance to the next. – kshetline Jun 25 '21 at 22:49
  • The timezone issue is that the way you're using `setHours()` depends completely on the default timezone where the code is running, which might not be the timezone `closeDeal` was collected in. – kshetline Jun 25 '21 at 22:51
  • "It fails" is not sufficient. Provide sample input, actual result and expected result. In *getEndDateTimeStamp*, setting the milliseconds to 999 then dividing by 1000 and rounding is equivalent to `currentTime.setHours(24,0,0,0)`. Similarly in *getStartDateTimeStamp*, because the hours are set to `0,0,0,0` rounding is redundant. – RobG Jun 26 '21 at 08:28

0 Answers0