web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Date Time issue when add Task using web API - Javascript

(0) ShareShare
ReportReport
Posted on by

Hi,

Here I'm creating a task using web api JS.

Dynamics CRM always saves "Scheduled Start" & "Scheduled End" as Local Date Time.  But I'm getting 13 hours 30 minutes difference between local datetime and saved DateTime.

var objAddTask = {};
var sd = new Date("2018-02-20T00:00:00");
var ed = new Date("2018-02-21T00:00:00");

objAddTask["scheduledstart"] = sd;
objAddTask["scheduledend"] = ed;

stringJSONTask = JSON.stringify(objAddTask);

var oDataSelect = serverUrl + "/api/data/v8.0/tasks";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: oDataSelect,
data: stringJSONTask,
beforeSend: function (XmlHttpRequest) {
XmlHttpRequest.setRequestHeader("Accept", "application/json");
XmlHttpRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
XmlHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=*");
XmlHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XmlHttpRequest.setRequestHeader("OData-Version", "4.0");
},
success: function (data, textStatus, XmlHttpRequest) {
alert("success");
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
}

 

Can anybody suggest me?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Date Time issue when add Task using web API - Javascript

    Hi Chhhaya,

    Could you please convert your date as UTC when you are passing.

    var d = new Date();

    var n = d.toUTCString();

    you can refer below link for more information -

    community.dynamics.com/.../dynamics-crm-datetimes-the-last-word

  • ashlega Profile Picture
    34,477 on at
    RE: Date Time issue when add Task using web API - Javascript

    Hi,

     wondering if that's consistent with the timezone offset configured in your "options" in Dynamics? In that case you may need to convert those values to UTC time and use timezone (Z):

    var sd = new Date("2018-02-20T13:30:00Z");

  • Community Member Profile Picture
    on at
    RE: Date Time issue when add Task using web API - Javascript

    Thanks Goutam!

    I tried that code, but It gives me an error 'Cannot convert the literal '2018-02-20T00:00:00' to the expected type 'Edm.DateTimeOffset'

    Can you suggest me another way?

  • Community Member Profile Picture
    on at
    RE: Date Time issue when add Task using web API - Javascript

    Thanks Alex!

    I have configured "Pacific Time US & Canada" timezone in options.

    var sd = new Date("2018-02-20T13:30:00Z");

    Can you please suggest me that how to convert "sd" (UTC datetime) to "Pacific Time US & Canada" timezone.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Date Time issue when add Task using web API - Javascript

    Hi Chaya,

    Your answer-  Convert UTC date time to Pacific Canada and US.

    var offset = -8; //For Canada and Pacific set offset

    var localdate = new Date(); //Local Time

    var utcConvertDate = localdate.toUTCString();  //Convert to UTC

    var currnetDate = new Date(utcConvertDate);  //Convert to date object

    var datePacific = new Date( currnetDate.getTime() + offset * 3600 * 1000).toUTCString().replace( / GMT$/, "" )

    console.log(datePacific);

    Hope this helps.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Community Member Profile Picture

Community Member 2

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans