Date field on catalog item can not be prior to the current day's date

Cupcake
Mega Guru

I have a date field where I want to ensure that the date selected from the date variable is not greater than the current days date. I have the following code for another date field that has to be 5 days out. Not very good with the date thing yet, so 1) should i have something like a (-1) in my dateMS portion of the script. And 2) will it render an error if the customer tries to select a date in the past or should I include some code that has an alert type of box stating something like "Please select a future date"......

var today = new Date();

var dateMS = today.getTime();

dateMS = dateMS + (5*24*60*60*1000);

var newDT = new Date();

newDT.setTime(dateMS);

g_form.setValue('ftr_needed_by',formatDate(newDT,g_user_date_format));

Thank you,

Karen

1 ACCEPTED SOLUTION

zica
Giga Guru

Karen,



Nothing wrong with the code, what happening is that 'getUTCDate() returns the day;


The reason that it did not allow you to select 2nd March is because 2 is lesser than 8 (current day) and conversely 16 (febr) is greater than 8 !!


I will suggest you to try with dateDiff() method to calculate the difference between two date/times given.



let me know if you have more concerns



Kind regards


Do not feel shy to mark correct or helpful answer if it helps or is correct



View solution in original post

18 REPLIES 18

zica
Giga Guru

Karen,



I am glad that your script is works


However, I think that does not cover every cases.


I explain : we are the 8th today. Could you please choose 2nd march 2016 as date on Needed_by_date field ? For this case, the field should be erased >> test successful


Now, could you choose 16th February 2016 as date ? Unless I am wrong, I am afraid this test wont succeed.



Could you please let me know ?



Kind regards,


Za


Do not feel shy to mark correct or helpful answer if it helps or is correct


I may not be fully understanding what you are trying to say. I only wanted to prevent someone from selecting a date prior to today's date. I did test it out, and I was not able to select (in your case/example) March 2nd. If I tried to select March 2nd - the form will not allow it, you would receive the error message and you will have to choose a date either today or any date after.



find_real_file.png         find_real_file.png



I did go back and selected February 16th and you are correct, it did allow me to select that day. I will go back and check the code again.


I hope I successfully answered your question for the most part.



Thank you,


Karen


zica
Giga Guru

Karen,



Nothing wrong with the code, what happening is that 'getUTCDate() returns the day;


The reason that it did not allow you to select 2nd March is because 2 is lesser than 8 (current day) and conversely 16 (febr) is greater than 8 !!


I will suggest you to try with dateDiff() method to calculate the difference between two date/times given.



let me know if you have more concerns



Kind regards


Do not feel shy to mark correct or helpful answer if it helps or is correct



VarunS
Kilo Sage

And what worked for me is the below client script.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // Get today's date, stripping off the time part
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // Create a date object from the newValue
    var selectedDate = new Date(newValue);
    selectedDate.setHours(0, 0, 0, 0); // Normalize time for accurate comparison

    // Check if the selected date is less than or equal to today's date
    if (selectedDate <= today) {
        // Show an alert if the selected date is not in the future
        alert("Date of desired ongoing to production cannot be in the past or today's date. Please select a future date.");

        // Clear the current value of the date field
        g_form.setValue('date_of_desired_ongoing_to_production', '');
    }
}