在数据表日期列中显示月份名称和年份

时间:2016-08-18 10:31:16

标签: javascript jquery ajax datatable

在我的数据表中,我有两个日期列显示数据,如2016年3月1日。我想删除当天,只想显示像2016年3月。

$('#bo_test_table').dataTable({
			"scrollY": 800,
			"scrollCollapse": true,
			"bServerSide" : true,
			"searching": false,
			"sAjaxSource" : "<Servlet URL>",
			"bProcessing" : true,
			"sPaginationType" : "full_numbers",
			"bJQueryUI" : true,
			"lengthMenu" : [ 25, 50, 100 ],
			"responsive": true,
			"bAutoWidth": true, 
			"oLanguage" : {
				"sProcessing" : "Loading. Please wait..."
			},
			"aoColumns": [
				{ "mDataProp": "boCountryName" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boSupplierName" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boProduct" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boUsageMonth" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boUsageValue" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boCurrDate" , "defaultContent": "<i>Not Available</i>"},
				{ "mDataProp": "boCurrency" , "defaultContent": "<i>Not Available</i>"},
	                  ],
			"aoColumnDefs": [ 
				{ 
					"aTargets": [4,5,6], 
					"orderable": false 
				},
				{ 
					"aTargets": [3,5], 
					"stype": "date", 
					"fnRender": function(data) {
						if(data != null) {
							var javascriptDate = new Date(data);
							javascriptDate = javascriptDate.getMonth() + 1 + "/" + javascriptDate.getFullYear();
							return "<div class='date'>"+javascriptDate+"<div>";
						}
					}
				}
			]
		});

我在以下link中找到了上述解决方案。但它显示日期为03/2016格式。

请帮忙。

2 个答案:

答案 0 :(得分:2)

您可以定义一个返回月份名称的数组

    var months = ["January", "February", "March", "April", "May", "June",
       "July", "August", "September", "October", "November", "December"
     ];
    months [javascriptDate.getMonth()+1]

答案 1 :(得分:0)

在前端尝试了很多后,我实际上解决了后端的问题。修改了实际的SQL脚本to_char(USAGE_MONTH,'MON-YYYY') as USAGE_MONTH。这解决了这个问题。