在Sapui5中使用日期

时间:2015-06-04 15:23:14

标签: javascript date sapui5

如何获取sapui5中的当前日期,当前年份,当前月份和当前周?这是我开始使用的代码:

var oType = new sap.ui.model.type.Date();
oType = new sap.ui.model.type.Date({ source: {}, pattern: "MM/dd/yyyy" });

我不知道从哪里开始。任何帮助将不胜感激。

编辑:如何将以下javascript函数添加到sapui5表中?

function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}

function dateFunction() {
    var today = new Date();
    var dd = addZero(today.getDate());
    var MM = addZero(today.getMonth() + 1);
    var yyyy = today.getFullYear();
    var hours = addZero(today.getHours());
    var min = addZero(today.getMinutes());
    var sec = addZero(today.getSeconds());
    var ampm = hours >= 12 ? 'PM' : 'AM';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'

    today = MM + '/' + dd + '/' + yyyy + "  " + hours + ":" + min + ":" + sec + " " + ampm;
}

1 个答案:

答案 0 :(得分:2)

获取当前日期

SAPUI5 中有 NO 预定义函数,因此使用原生JavaScript方法:

var oDate = new Date(); 

如何在表格中添加日期?

JS Fiddle

var oData = {
    results: [{
        name: "Today",
        date: new Date()
    }, {
        name: "Someday",
        date: new Date("2015/01/01")
    }, {
        name: "New Year",
        date: new Date("2016/01/01")
    }]
}

var oModel = new sap.ui.model.json.JSONModel(oData);


// create table:
var oTable = new sap.m.Table({
    columns: [
    new sap.m.Column({
        header: new sap.m.Label({
            text: "When"
        })
    }),
    new sap.m.Column({
        header: new sap.m.Label({
            text: "Date"
        })
    })]
});
var oType = new sap.ui.model.type.Date({
    pattern: "MM/dd/yyyy"
});
var oTemplate = new sap.m.ColumnListItem({
    cells: [
    new sap.m.Text({
        text: "{name}"
    }),
    new sap.m.Text({
        text: {
            path: 'date',
            type: oType
        }
    })]
});

oTable.setModel(oModel);
oTable.bindItems("/results", oTemplate);
oTable.placeAt("content");

更新:根据评论请求 所有你需要的是:

 var oType = new sap.ui.model.type.Date({
        pattern: "MM/dd/yyyy"
    });
oTable.addColumn(new sap.ui.table.Column("today", {
    label: new sap.m.Label({
        text: {
            path: 'today',
            type: oType
        }
    })
    sortProperty: 'today',
    filterProperty: 'today'
}));