2015-06-04 40 views
1

如何在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; 
} 
+0

你應該看看如何獲​​得當前日期在JavaScript –

+0

好。你知道我會如何將它插入sapui5表嗎? – Anthony

+0

插入什麼?要在ui5表格中插入任何內容,您需要將一個項目添加到表格的行集合中。可以通過綁定或addRow ..完全是什麼問題? –

回答

2

來獲取當前日期

NOSAPUI5預定義的功能,因此使用本地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' 
})); 
+0

我創建瞭如下的列:'oControl = new sap.m.Text({text:「{today}」}); oTable.addColumn(new sap.ui.table.Column(「today」,{ label:new sap.m.Label({text:「today」}), template:oControl, sortProperty:「today」, filterProperty:「today」 }));'我已經得到它的格式如下:星期五2015年6月5日09:32:45 GMT-0400(東部夏令時)我如何得到它在模式「MM/dd/yyyy」我試過了:'oControl = new sap.m.Text({text:{path:'{today}',type:oType}});'但是沒有工作 – Anthony

+0

I已經嘗試了更新。我今天右側的所有數據都向左移動了一列。我認爲那是因爲'template'丟失了?我添加了模板,但仍然無法正確格式化。 – Anthony

+0

請添加整個頁面,以便我編輯並正確回答您的問題 –