在数据库中,格式是正确的,但在ts请求中,它是以这种方式到达的,我如何更正它?

cols: any[] = [
  {
    field: 'dataHoraInicio',
    header: 'Início',
    fieldback: 'dataHoraInicio'
  },
  {
    field: 'dataHoraFim',
    header: 'Final',
    fieldback: 'dataHoraFim'
  }
]

我唯一发现的就是对固定代码进行格式化

推荐答案

创建一个TypeScrip(TS)函数,以字符串形式返回格式化的日期:

function formatDate(inputDate) {
  // Convert the input string to a Date object
  const date = new Date(inputDate);

  // Get the day with leading zeros
  const day = String(date.getDate()).padStart(2, '0');

  // Get the month with leading zeros
  const month = String(date.getMonth() + 1).padStart(2, '0');

  // Get the year
  const year = date.getFullYear();

  // Get the hours with leading zeros
  const hours = String(date.getHours()).padStart(2, '0');

  // Get the minutes with leading zeros
  const minutes = String(date.getMinutes()).padStart(2, '0');

  // Format the date as dd/MM/yyyy HH:mm
  const formattedDate = `${day}/${month}/${year} ${hours}:${minutes}`;

  return formattedDate;
}

现在使用上面的函数获得格式化的日期,如下所示:

const inputDate = '2023-09-18T12:52:45.000Z';
const formattedDate = formatDate(inputDate);
console.log(formattedDate); // Output: "18/09/2023 18:22"

function formatDate(inputDate) {
  // Convert the input string to a Date object
  const date = new Date(inputDate);

  // Get the day with leading zeros
  const day = String(date.getDate()).padStart(2, '0');

  // Get the month with leading zeros (Note: Months are zero-based)
  const month = String(date.getMonth() + 1).padStart(2, '0');

  // Get the year
  const year = date.getFullYear();

  // Get the hours with leading zeros
  const hours = String(date.getHours()).padStart(2, '0');

  // Get the minutes with leading zeros
  const minutes = String(date.getMinutes()).padStart(2, '0');

  // Format the date as dd/MM/yyyy HH:mm
  const formattedDate = `${day}/${month}/${year} ${hours}:${minutes}`;

  return formattedDate;
}

const inputDate = '2023-09-18T12:52:45.000Z'; // Replace this with desired date
const formattedDate = formatDate(inputDate);
console.log(formattedDate);

Typescript相关问答推荐

TS 2339:属性切片不存在于类型DeliverableSignal产品[]上>'

当类型断言函数返回假时,TypScript正在推断从不类型

在这种情况下,如何获得类型安全函数的返回值?

类型脚本泛型最初推断然后设置

如何在TypeScrip面向对象程序设计中用方法/属性有条件地扩展类

TypeScrip:基于参数的条件返回类型

APIslice-CreateAPI的RTK打字错误

是否有一个版本的联合类型递归地使属性只出现在一个可选选项中?

我可以使用TypeScrip从字符串词典/记录中填充强类型的环境对象吗?

在类型脚本中将泛型字符串数组转换为字符串字母并集

如何使这种一对一关系在旧表上起作用?

将超类型断言为类型脚本中的泛型参数

Angular NG8001错误.组件只能在一个页面上工作,而不是在她的页面上工作

作为参数的KeyOf变量的类型

导航链接不触发自定义挂钩

Karma Angular Unit测试如何创建未解析的promise并在单个测试中解决它,以判断当时的代码是否只在解决后运行

定义一个只允许来自另一个类型的特定字符串文字的类型的最佳方式

剧作家测试未加载本地网址

如何在 TypeScript 类型定义中简化定义长联合类型

为什么 typescript 在对象合并期间无法判断无效键?