[英]How to change the date format output in javascript code/function?
I have javascript function that pull data from the database and give the date output. 我有JavaScript函数,可从数据库中提取数据并提供日期输出。 This part of the output scripts: `data: { columnsDef: ['letter_number', 'letter_date', 'job_desc'], }, },
输出脚本的这一部分:`data:{columnDef:['letter_number','letter_date','job_desc'],},},
columns: [
{ data: 'letter_number', name: 'letter_number' },
{ data: 'letter_date', name: 'letter_date' },
{ data: 'job_desc', name: 'job_desc' },
],`
The letter_date output was 2019-04-29 and I intend to change the format to 29-04-2019 , I've already tried some javascript dateformat function on the top of the scripts but the page just blank no output showed on the page. letter_date的输出为2019-04-29 ,我打算将格式更改为29-04-2019 ,我已经在脚本顶部尝试了一些javascript dateformat函数,但是页面空白,页面上未显示任何输出。 I'm using laravel and using Carbon library for other date format in laravel code tag and there are no problem.
我正在使用laravel,并在laravel代码标签中将Carbon库用于其他日期格式,所以没有问题。
I'm confused how to change the format inside the Javascript output code like shown above { data: 'letter_date', name: 'letter_date'}
. 我很困惑如何更改Javascript输出代码内的格式,如上面的
{ data: 'letter_date', name: 'letter_date'}
。
Any solution I'll appreciate it very much. 任何解决方案,我将不胜感激。 Thank you.
谢谢。
If the format is consistent, you could split on each dash and rejoin them in the desired format: 如果格式一致,则可以在每个破折号上分开并以所需的格式重新加入它们:
const date = '2019-04-29' const dateArr = date.split('-') const newDate = `${dateArr[2]}-${dateArr[1]}-${dateArr[0]}` console.log(newDate)
Use moment to format your date as you want 根据需要使用时刻来格式化日期
This is the most robust display option.
这是最可靠的显示选项。 It takes a string of tokens and replaces them with their corresponding values.
它接受一串标记并将其替换为其相应的值。
moment(new Date()).format(); // "2014-09-08T08:02:17-05:00" (ISO 8601, no fractional seconds)
moment(new Date()).format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
in your context: 在您的上下文中:
moment(new Date()).format('DD-MM-YYY');
I think you used DataTable plugin for showing data in grid. 我认为您使用DataTable插件在网格中显示数据。 you can change values in createdRow.
您可以在createdRow中更改值。
...
columns: [
{ data: 'letter_number', name: 'letter_number' },
{ data: 'letter_date', name: 'letter_date' },
{ data: 'job_desc', name: 'job_desc' },
],
createdRow: function(nRow, aData, iDataIndex){
const d= aData.letter_date.split('-');
$('td:eq(1)',nRow).html(d[2]+'-'+d[1]+'-'+d[0]);
}
...
I got this solution, first of all I'm using datatables plugin in my laravel. 我得到了这个解决方案,首先我在Laravel中使用了datatables插件。 Using Yazra datatables, and use Carbon libraries to convert the date format.
使用Yazra数据表,并使用Carbon库转换日期格式。 I found this solution on github posts.
我在github帖子上找到了这个解决方案。 This the link: DateTime Format Yazra Datatables and Carbon Plugin in laravel
此链接: Laravel中的DateTime格式Yazra数据表和Carbon插件
This working great, and actually there are connection between my Controller on laravel and Column on Javascript as used on Yazra datatables. 这个工作很好,实际上,在Yalar数据表上使用的laravel上的Controller和Javascript上的Column之间确实存在连接。 Hope this help anyone.
希望这对任何人有帮助。
Thank you so much for all the solution above...Thank you. 非常感谢您提供上述所有解决方案...谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.