I want to calculate the extra charge according to the date and time

[ad_1]

Hello @furuyana

Javascript, the programming language supported by browsers, is case-sensitive. Your equation confuses the IF operation with the if conditional statement.

By reading your equation, I cannot check the structure of the fields on your form. However, assuming you have two date-time fields, fieldname1, and fieldname2 (the time’s components in the fieldname1 and fieldname2 would be applied to every day in the date/time interval). The price calculation must satisfy the following conditions:

Monday to Friday: 1200 per day,
Saturday, Sunday, and holidays: 1500 per day.
Price per hour early than 9 AM or after 5 PM: 300 per hour.
Holidays: January 1st, May 1st, and December 25th.

The equation that satisfies the hypothetical project’s requirements can be implemented as follows:

(function(){
  var from = MIN(fieldname1, fieldname2),
      to = MAX(fieldname1, fieldname2),
      holidays = ('01/01', '05/01', '12/25'),
      extra_hours = 0,
      total = 0;

  extra_hours = MAX(0, 9 - HOURS(from))+MAX(0,HOURS(to)-17)*(to-from);

  while(from <= to) {
    if( OR( WEEKDAY(from) == 1, WEEKDAY(from) == 7, holidays.indexOf(CONCATENATE(MONTH(from),'/',DAY(from)))!= -1)){
      total += 1500;
    } else {
      total += 1200;
    }
    from++;
  }

  return PREC(total+extra_hours*300, 2);
})()

Best regards.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer