javascript 在 Sapui5 中处理日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30647921/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:29:18  来源:igfitidea点击:

Working with Dates in Sapui5

javascriptdatesapui5

提问by Anthony

How can I get the current date, current year, current month, and current week in sapui5? This is the code I started with:

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

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

I have no idea where to go from here. Any help would be greatly appreciated.

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

EDIT: How do I get the following javascript function into a sapui5 table?

编辑:如何将以下 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;
}

回答by Sunil B N

To get Current Date:

获取当前日期

there is NOpredefined function in SAPUI5, hence use native JavaScript Method:

NO预定义的功能在SAPUI5,因此使用本地JavaScript方法:

var oDate = new Date(); 


How to put date in a table?

如何在表格中输入日期?

JS Fiddle

JS小提琴

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");

Update: Based on comment requestAll you need is this:

更新:根据评论请求您所需要的就是:

 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'
}));