Skip to main content

Notifications

Announcements

No record found.

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

  • PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: Compare datetime with date field

    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

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Compare datetime with date field

    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

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 Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,162 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans