【问题标题】:What is the name of the date format and how do I display just the date in html ? Mon Aug 24 2020 00:00:00 GMT+0800 (Singapore Standard Time)日期格式的名称是什么,如何仅在 html 中显示日期? 2020 年 8 月 24 日星期一 00:00:00 GMT+0800(新加坡标准时间)
【发布时间】:2020-12-12 22:54:46
【问题描述】:

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

【问题讨论】:

标签: javascript html postgresql datetime datetime-format


【解决方案1】:

在 postgres 中,您可以使用 to_char 将日期转换为您想要的格式。 例如

select to_char('24-08-2020'::date, 'dd/mm/yyyy'::text) AS mydate

结果

24/08/2020

【讨论】:

  • 感谢您的回复。这在 pgadmin 工具中有效。我得到这个:to_char 列下的“24/08/2020”。但是,当我在 js 中添加这个查询时,出现了语法错误。
  • 它是 postgres 中的一个查询,您必须在后端执行收集数据,而不是在 js 或前端执行。
【解决方案2】:

您可以将其转换为 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多