admin管理员组

文章数量:1353897

I am trying to pull out a date from one field, modify that date and set that value into another, here is my code:

var startDate = Xrm.Page.getAttribute('new_startdate').getValue();
var expiryDate = startDate.getDate()+3; //Add 3 days

var expiryField = Xrm.Page.getAttribute('new_expirydate').setValue(expiryDate);

Can someone explain what I'm doing wrong? I've been at this for a while now and I fear I'm missing something blatently obvious..

Thanks in advance.

EDIT;

When the script fires, 1/1/1970 is set in the expiry field.

I am trying to pull out a date from one field, modify that date and set that value into another, here is my code:

var startDate = Xrm.Page.getAttribute('new_startdate').getValue();
var expiryDate = startDate.getDate()+3; //Add 3 days

var expiryField = Xrm.Page.getAttribute('new_expirydate').setValue(expiryDate);

Can someone explain what I'm doing wrong? I've been at this for a while now and I fear I'm missing something blatently obvious..

Thanks in advance.

EDIT;

When the script fires, 1/1/1970 is set in the expiry field.

Share Improve this question edited Jan 11, 2013 at 13:34 Jack Nutkins asked Jan 11, 2013 at 12:54 Jack NutkinsJack Nutkins 1,5555 gold badges38 silver badges73 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

this should work

var startDate = Xrm.Page.getAttribute('new_startdate').getValue();
var expiryDate = new Date();
expiryDate.setDate(startDate.getDate()+3); //Add 3 days

var expiryField = Xrm.Page.getAttribute('new_expirydate').setValue(expiryDate);

Your problem is that the function getDate() returns the day of the month. So the result of

var startDate = new Date("January 11, 2013");
var expiryDate = startDate.getDate()+3;

would be the number 14.

I assume that this gets converted to a Date using the Date(milliseconds) overload which represents

Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch).

Therefore you see the this value.

So, the solution which lazarus has posted shows the correct approach.

本文标签: CRM 2011 JavaScriptModify Date From One Field And Set To AnotherStack Overflow