javascript Crm 2011 从表单上的 DateTime 字段获取时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14029741/
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 Getting time from DateTime field on form
提问by tdgtyugdyugdrugdr
I'm having trouble getting the hour and minute portions from the datetime field on the form.
我无法从表单上的日期时间字段中获取小时和分钟部分。
var date = Xrm.Page.data.entity.attributes.get("createdon").getValue();
I can use getDate(), getMonth() and GetFullYear() methods on the returned object, but I cannot get the hour and minute data. The date object shows "Tue Dec 25 11:47:44 UTC+0200 2012", so the time is there. Is the only way to get hour/minute to do some ugly substring operations on that value?
我可以在返回的对象上使用 getDate()、getMonth() 和 GetFullYear() 方法,但我无法获取小时和分钟数据。日期对象显示“Tue Dec 25 11:47:44 UTC+0200 2012”,所以时间就在那里。是让小时/分钟对该值进行一些丑陋的子字符串操作的唯一方法吗?
Thanks in advance.
提前致谢。
采纳答案by Konrad Viltersten
You could use getHoursand getMinuteson the Dateobject.
您可以在Date对象上使用getHours和getMinutes。
var date = new Date();
var time = date.getHours() + ":" + date.getMinutes();
Now, you only need to substitute the value for dateas it in my example is the current point of time and you're fetching it from a field.
现在,您只需要替换日期的值,因为在我的示例中它是当前时间点,并且您正在从字段中获取它。
You might want consider shortening your syntax as well. I'm not in front of the computer at the moment (not thatcomputer, that is), but I believe that you can go like this instead.
您可能还需要考虑缩短语法。我现在不在电脑前(不是那台电脑),但我相信你可以这样去。
var date = Xrm.Page.getAttribute("createdon").getValue();
It's amazing - no matter how much JavaScript one codes. Five minutes later, everything is gone from the mind. It's like if the syntax is intended to be memory resistant. :)
这太神奇了——不管 JavaScript 代码有多少。五分钟后,一切都从脑海中消失了。就像语法旨在抗内存一样。:)
You should notbe playing around with substringnor regex. JavaScript source code tends to be ugly and confusing the way it is already. No point making it worse and less comprehensible.
您不应该使用substring或regex。JavaScript 源代码往往是丑陋的,并且令人困惑。没有必要让它变得更糟,更难以理解。
And as for the regular expressions - a programmer had a problem to solve. So he thought - "I know, I'll use RegEx". Then he had twoproblems.
至于正则表达式——程序员有一个问题要解决。所以他想——“我知道,我会使用RegEx”。然后他有两个问题。
回答by Amila Rukshan
var DateTimeFormatString ="hh:MM:ss";
// Formats the date to CRM format
function dateToCRMFormat(date) {
return dateFormat(date, DateTimeFormatString );
}
// Formats the date into a certain format
function dateFormat(date, format) {
var f = "";
try {
f = f + format.replace(/dd|mm|yyyy|MM|hh|ss|ms|APM|\s|\/|\-|,|\./ig,
function match() {
switch (arguments[0]) {
case "dd":
var dd = date.getDate();
return (dd < 10) ? "0" + dd : dd;
case "mm":
var mm = date.getMonth() + 1;
return (mm < 10) ? "0" + mm : mm;
case "yyyy": return date.getFullYear();
case "hh":
var hh = date.getHours();
return (hh < 10) ? "0" + hh : hh;
case "MM":
var MM = date.getMinutes();
return (MM < 10) ? "0" + MM : MM;
case "ss":
var ss = date.getSeconds();
return (ss < 10) ? "0" + ss : ss;
case "ms": return date.getMilliseconds();
case "APM":
var apm = date.getHours();
return (apm < 12) ? "AM" : "PM";
default: return arguments[0];
}
});
}
catch (err) {
}
return f;
}
For testing
供测试用
var time= Xrm.Page.getAttribute("createdon").getValue();
alert("Time: " + dateToCRMFormat(time));