web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Comparing date field with current date Javascript

(0) ShareShare
ReportReport
Posted on by 2,665

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

I have the same question (0)
  • Verified answer
    Prashant Maurya Profile Picture
    236 on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans