Skip to main content

Notifications

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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Comparing date field with current date Javascript

    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.

  • Verified answer
    Prashant Maurya Profile Picture
    236 on at
    RE: Comparing date field with current date Javascript

    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

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans