CRM 2011 JavaScript - 从一个字段修改日期并设置为另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14278633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CRM 2011 JavaScript - Modify Date From One Field And Set To Another
提问by Hyman Nutkins
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.
当脚本触发时,会在到期字段中设置 1/1/1970。
回答by lazarus
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);
回答by ccellar
Your problem is that the function getDate()
returns the day of the month. So the result of
您的问题是该函数getDate()
返回一个月中的哪一天。所以结果
var startDate = new Date("January 11, 2013");
var expiryDate = startDate.getDate()+3;
would be the number14.
将是数字14。
I assume that this gets converted to a Date using the Date(milliseconds) overload which represents
我假设这使用表示的 Date(milliseconds) 重载转换为 Date
Therefore you see the this value.
因此,您会看到此值。
So, the solution which lazarus has posted shows the correct approach.
因此,lazarus 发布的解决方案显示了正确的方法。