1

任何人都知道如何调整当前的 JS 代码并使其 [ItemDate] 以 dd/MM/yyyy 格式而不是默认格式显示日期:MM/dd/yyyy HH:mm:ss

该代码是从将 XML 转换为可读 HMTL 格式的网站复制而来的……麻烦只是 date.format 我发现很难实现更改。

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>  
<script type="text/javascript">  
SPClientTemplates.TemplateManager.RegisterTemplateOverrides({  
  Templates: {  
           Fields: {  
                'ItemsOverview': {   
                    'View': repeatingSectionViewTemplate  
                 }  
           }  
  }  
});  

function repeatingSectionViewTemplate(ctx) {  
   var xml = ctx.CurrentItem["ItemsOverview"];  
   var decodedxml = xml.DecodeXMLNotation();   
   var htm = "";  

   xmlDoc = $.parseXML( decodedxml );  
   $xml = $( xmlDoc );  
   $xml.find("Item").each(function() {  
      htm = htm + "<tr><td width='50px'>" + $(this).find("ItemNumber").text() + "</td><td width='140px'>" + $(this).find("ItemDescription").text() + "</td><td width='70px'>" + $(this).find("ItemStatus").text() + "</td><td>" + $(this).find("ItemDate").text()
 +"</td><td>" + $(this).find("CollectedByUser").text() +"</td></tr>"; 
   });  

   return "<table border='1px' width='550px'  style='border-collapse:collapse;'><tr><th align='left' width='50px'>Item</th><th align='left' width='180px'>Description</th><th align='left' width='70px'>Status</th><th align='left' width='70px'>Date</th><th align='left' width='170px'>Collected By</th></tr>" + htm +"</table>";  
};  

//Replaces html notation to their equivalent xml escape characters.  
String.prototype.DecodeXMLNotation = function () {  
   var output = this;  
    if ($.trim(output) != "") {  
        output = output.replace(/&apos;/g, "'").replace(/&quot;/g, '"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&amp;/g, '&');  
    }  
    else {  
        output = "";  
    }  
    return output;  
};  

</script>
4

4 回答 4

1

我认为“dd/MM/yyyy”是法语格式:

var date = new Date('12/17/2018 03:24:00');

console.log(date.toLocaleDateString("fr-FR"));

于 2019-04-19T10:33:48.523 回答
1

您必须创建一个为您重新格式化日期的函数。

function formatDate(dateStr) {
    const d = new Date(dateStr);
    return d.getDate().toString().padStart(2, '0') + '/' + d.getMonth() + 1 + '/' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes().toString().padStart(2, '0');
}

而不是$(this).find("ItemDate").text()你会使用formatDate($(this).find("ItemDate").text())

于 2019-04-19T10:16:06.640 回答
0

使用Moment.js 插件
安装使用:

npm install moment --save

然后使用

var moment = require('moment');
let formatted = moment().format('DD/MM/YYYY');

在你的文件中。

或者,如果您是内联的,html则使用:

<script src="moment.js"></script>
<script>
    let formatted = moment().format('DD/MM/YYYY');
</script>

或者
您可以使用以下转换为相同格式的函数:

parseDate = (d) => {
        let date = new Date(d);        
        var mm = date.getMonth() + 1; // getMonth() is zero-based
        var dd = date.getDate();

        let newdate = [(dd>9 ? '' : '0') + dd + '/',
                    (mm>9 ? '' : '0') + mm+  '/',
                    date.getFullYear(),
                   ].join('');
        return newd + ' at ' + date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds();
    }

希望这可以帮助。

于 2019-04-19T10:31:56.137 回答
0

最好的方法是 Moment.js 插件。moment().format('DD/MM/YYYY);

var newDate = new Date();
console.log(newDate);
var formattedDate = moment().format('DD/MM/YYYY');
console.log(formattedDate);
<script src="https://momentjs.com/downloads/moment.js"></script>

于 2019-04-19T10:18:47.797 回答