Hi,
I am writing a script to check a event date field with current date and if it is less than or equal to day, I am trying to do something with the other fields.
But my script is not working as expected. I did debug the script to check if I am getting the correct dates and seems to be fine. The condition which checks for less than or equal to seems to be ignored even if the date field has a date that is less than today.
Can somebody please guide me through this?
Here is my script:
if(results!=null){ var dateTimeValue=results.value[0].new_datetime;//On debug, this is the date that I am getting 2018-05-08T13:30:00Z var today=new Date();//today's date Mon May 14 2018 09:50:50 GMT-0400(Eastern Daylight Time) if(eventdateTimeValue<=today){ //do something } }
Thanks for any help.
*This post is locked for comments
Hi Roxana ,
Try to implement below , here I have prepared for both date and date time comparison . Hope this helps -
function myFunction() { var dateTimeValue = results.value[0].new_datetime; // "Mon May 14 2018 09:50:50 GMT-0400"; var crmDateValue = new Date(dateTimeValue); //var eventdateTimeValue = crmDateValue.getFullYear() + '/' + (crmDateValue.getMonth() + 1) + '/' + crmDateValue.getDate(); // Uncomment Only For Date Comparison var todaydate = new Date(); // var today = todaydate.getFullYear() + '/' + (todaydate.getMonth() + 1) + '/' + todaydate.getDate(); // Uncomment Only For Date Comparison if (crmDateValue <= todaydate) { //You can compare only date - using this need to replace - if (eventdateTimeValue <= today) alert("event date less than equal today"); } else { alert("event date greater than equal today"); } }
Hope this helps.
Hi
1. You can set header to get formatted value if you are using Web API, "GET" request.
2. when you get the response then extract date value as "var crmDateValue= new Date(results.value[0]["new_datetime"])".
3. get current date value as "var today=new Date();"
4. Now parse and compare both value as shown below.
if ((Date.parse(crmDateValue) <= Date.parse(today))) {
//your logic goes here
}
Please mark this as answer :)
Thanks-
Prashant
Mohamed Amine Mahmoudi
83
Super User 2025 Season 1
Community Member
54
Victor Onyebuchi
6