Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

datetime string to datetime

Posted on by 110

Hi

   I am retrieving a datetime value using webapi which is retrieved as a string. How can I convert it into Datetime type and then compare it with a date in a date only field(comparing datetime with date) and store as a date in dynamics crm using javascript.

*This post is locked for comments

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: datetime string to datetime

    After you get your datetimestring for the api, you can create the date by calling the following method:

    var newDate = new Date(apiDateField);

    You can then retrieve the value of the other date field by using the standard CRM functions:

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

    Finally you can then use standard logic to compare two date fields:

    if (newDate > dateField2) ...

    You can also use the dates.compare javascript function from the library link below:

    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

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: datetime string to datetime

    Hi Sathish,

    If you are retrieving a datetime value using web api then it should be returned as datetime not string. Once retrieve you can use JS methods to convert it to date only.

    something like this-

    var createdon = <value retrieved from web api>

    var d = new Date(createdon);

    var dateOnly = new Date(d.getYear(),d.getMonth(),d.getDate(),0,0,0,0)

    Refer this link for JavaScript date reference

    www.w3schools.com/.../jsref_obj_date.asp

    Hope this helps.

  • Suggested answer
    PranavShroti Profile Picture
    PranavShroti 4,510 on at
    RE: datetime string to datetime

    Hi Sathish,

    As per my limited knowledge of webAPI, there is no direct way of doing this. If string is returned then you have to convert it using JS functions.

    Check out this link: stackoverflow.com/.../converting-string-to-date-in-js

    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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans