Skip to main content

Notifications

Announcements

No record found.

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 Microsoft Employee

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

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 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.

  • Community Member Profile Picture
    Community Member Microsoft Employee 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.

  • Community Member Profile Picture
    Community Member Microsoft Employee 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?

  • ashlega Profile Picture
    ashlega 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");

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 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

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…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans