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

Announcements

No record found.

News and Announcements icon
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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans