moment.locale('fr'); // Load French
var frenchMonths = ('janvier,février,mars,avril,mai,juin,' +
'juillet,août,septembre,octobre,novembre,décembre').split(',');
generateRows(document.getElementById('date-table'), frenchMonths.map(function(month) {
return '29 ' + month + ' 2016'; // Format the month name into a valid date-string
}), function(dateString) {
return [
dateString,
new Date(dateString),
moment(dateString, 'DD MMMM YYYY').toDate()
];
});
function generateRows(table, data, fn) {
return appendChildren(table, data.map(function() {
return createRow(fn.apply(null, arguments))
}));
}
function createRow(data) {
return appendChildren(document.createElement('tr'), data.map(function(value) {
return createCell(value);
}));
}
function createCell(value) {
var td = document.createElement('td');
td.innerHTML = new String(value).substring(0, 34);
if (value instanceof Date && !isValidDate(value)) td.className = 'invalid';
return td;
}
function appendChildren(parent, nodes) {
for (var i = 0; i < nodes.length; i++) { parent.appendChild(nodes[i]); }
return parent;
}
function isValidDate(date) {
if (Object.prototype.toString.call(date) === '[object Date]') {
return !isNaN(date.getTime()); // d.valueOf() could also work
} else {
return false;
}
}
body {
font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,sans-serif;
font-size: 0.8em;
}
table, tr, th, td {
border-collapse: collapse;
border: thin solid #000;
}
.invalid {
color: #F00;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/locale/fr.js"></script>
<table id="date-table">
<thead>
<tr>
<th>Input</th>
<th>Output - Date Casting</th>
<th>Output - MomentJS Parse</th>
</tr>
</thead>
<tbody></tbody>
</table>
你是如何結合'demand.event_mindate'?輸入日期字符串是什麼樣的? –
這是Django模板語言。當我打印ad1和d2時,我得到了d1打印,但是d2我得到了無效日期。 – user3232446