首页 > 解决方案 > 日期格式的名称是什么,如何仅在 html 中显示日期?2020 年 8 月 24 日星期一 00:00:00 GMT+0800(新加坡标准时间)

问题描述

我正在使用 postgresSQL 数据库。我添加了一个日期列,在 pgadmin 中插入一条记录后,日期字段看起来像“24-08-2020”。但是,当我尝试从数据库中渲染/检索并在 html 中显示时,日期字段看起来像这样“Mon Aug 24 2020 00:00:00 GMT+0800 (Singapore Standard Time)”。我只想以这种格式 dd/mm/yyyy 显示日期。postgres 中是否有任何查询,或者我应该在 js 上工作?

标签: javascripthtmlpostgresqldatetimedatetime-format

解决方案


您可以将其转换为 unix 时间戳并使用它。使用 Date 类函数取出日、月、年。

const allmonths = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
const day = (d) => {
  if (d < 10)
    return '0' + d;
  return d;
};
const dt = new Date('Mon Aug 24 2020 00:00:00 GMT+0800')
const dtFormat = day(dt.getDate()) + '-' +allmonths[dt.getMonth()] + '-' + dt.getFullYear() // 24-08-2020

推荐阅读