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)

Compare datetime with date field

(0) ShareShare
ReportReport
Posted on by 110

Hi

How to compare a datetime field with a date field using javascript? Any suggestions?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    You can get the date part of the field by using the getFullYear, getMonth and getDate methods of the date field as follows:

    var dateTimeField = Xrm.Page.getAttribute("bgx_startdate").getValue();

    var yearValue = dateTimeField.getFullYear();

    var monthValue = dateTimeField.getMonth(); // You will need to add 1 to get the current month as this is 0 based.

    var dayValue = dateTimeField.getDate();

    You can then get the date from these fields by calling the new Date function

    var newDate = new Date(yearValue, motnhValue, dayValue);

    You can then use standard logic to compare two date fields:

    var dateField2 = Xrm.Page.getAttribute("bgx_enddate").getValue();

    if (newDate > dateField2) ...

    You can also use the dates.compare javascript function:

    dates.compare(dateField1, dateField2)

    This will return the following:

    -1 if dateField1 < dateField2

    0 if the dates are the same

    1 if dateField1 > dateField2

    See stackoverflow link below:

    stackoverflow.com/.../497790

  • PranavShroti Profile Picture
    4,510 on at

    Hi Sathish, as pointed out by Aric, you will have to extract the date value from DateTime field then only it will be comparable.

    Regards,

    Pranav

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