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)

datetime string to datetime

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    PranavShroti Profile Picture
    4,510 on at

    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

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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

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