数据表JS默认日期格式

时间:2020-02-13 06:54:15

标签: javascript jquery datatables

我需要更改默认日期格式,因为我有1000多个数据表实例,所以不想在每个实例中更改它,而是要更改其默认日期格式呈现方式

我正在尝试在渲染时替换文本,我正在尝试按照以下方式进行操作

 $.fn.dataTable.render.text(function (data, type, row) {
        console.log('in')
        console.log(data)
        console.log(type)
        console.log(row)
    });
    $.extend($.fn.dataTableExt.render, {
        "text": function (a) {
            console.log(a)
        },
    });

我正在尝试获取数据,然后可以检查数据类型并替换日期格式并返回新的格式化数据,

但呈现数据表时,以上都不是调用

我也尝试过

$.fn.dataTable.moment('dddd D MMMM YYYY');

但没有效果

请帮助 谢谢

1 个答案:

答案 0 :(得分:0)

单独使用javascript,请尝试以下操作...

export class CarComponent implements OnInit {
  private _defaultCar: car = {
    // default isCar is true
    isCar: true,
    // default wheels  will be 4
    wheels: 4
  };

  @Input() newCar: car = {};

  constructor() {}

  ngOnInit(): void {

   // this will concate both the objects and the object declared later (ie.. ...this.newCar )
   // will overwrite the default value. ONLY AND ONLY IF DEFAULT VALUE IS PRESENT

    this.newCar = { ...this._defaultCar, ...this.newCar };
   //  console.log(this.newCar);
  }
}

然后这样称呼它...

function formatEmptyDates(t) {
  if(t) {
    var monthNames = [
      "January", "February", "March",
      "April", "May", "June", "July",
      "August", "September", "October",
      "November", "December"
    ];

    var date = new Date(t);
    var day = date.getDate();
    var monthIndex = date.getMonth();
    var year = date.getFullYear();

    //console.log(day, monthNames[monthIndex], year);
    return day + ' ' + monthNames[monthIndex] + ' ' + year;
  } else {
    return 'Unknown';
  }
}

对于空行而言,它可能很有价值,但是您可以轻松地根据需要对其进行调整。