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
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:
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.
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
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156