3

我无法从表单上的日期时间字段中获取小时和分钟部分。

var date = Xrm.Page.data.entity.attributes.get("createdon").getValue();

我可以对返回的对象使用 getDate()、getMonth() 和 GetFullYear() 方法,但我无法获取小时和分钟数据。日期对象显示“Tue Dec 25 11:47:44 UTC+0200 2012”,所以时间就在那里。获得小时/分钟的唯一方法是对该值进行一些丑陋的子字符串操作吗?

提前致谢。

4

2 回答 2

3

您可以在Date对象上使用getHoursgetMinutes 。

var date = new Date();
var time = date.getHours() + ":" + date.getMinutes();

现在,您只需要将值替换为日期,因为在我的示例中它是当前时间点,并且您正在从字段中获取它。


您可能还需要考虑缩短语法。我现在不在电脑前(不是台电脑,也就是说),但我相信你可以这样去。

var date = Xrm.Page.getAttribute("createdon").getValue();

太神奇了——不管 JavaScript 代码有多少。五分钟后,一切都从脑海中消失了。就像语法旨在抗记忆一样。:)


应该玩弄substringregex。JavaScript 源代码往往是丑陋的,而且已经令人困惑。没有必要让它变得更糟,更难以理解。

至于正则表达式 - 程序员有一个问题要解决。所以他想——“我知道,我会使用RegEx ”。然后他有两个问题。

于 2012-12-25T21:36:39.783 回答
3
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;
}

供测试用

var time= Xrm.Page.getAttribute("createdon").getValue();
 alert("Time: " + dateToCRMFormat(time));
于 2013-10-17T12:18:23.703 回答