# 如何让帖子时间戳显示秒数？

**URL:** <https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390>\
**Category:** Support\
**Created:** [2024年三月28日 18:42 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390 "2024-03-28T18:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![orangeandblack5](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@orangeandblack5](https://meta.discourse.org/u/orangeandblack5)\
**Post date:** [2024年三月28日 18:42 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390/1 "2024-03-28T18:42:30Z")

</div>

我在网上找了找，但什么也没找到——在分享弹出窗口中显示帖子发布时间（精确到秒）的最简单方法是什么？

 ![image](https://global.discourse-cdn.com/meta/original/4X/d/e/a/dea02b2cd59530e62eeff313bd35aedf1ae614aa.png)

每个帖子在 HTML 中都有确切的时间戳，所以这些信息肯定存在，但我不太确定更新此文本（不幸的是，它似乎是硬编码的，而不是格式化的日期）以添加秒的最佳方法是什么。我主要熟悉 CSS，不太熟悉 JavaScript（我能读懂并理解它，但不够精通，不知道如何“正确”开始解决这个问题 [我大部分的编码经验都在嵌入式领域]），所以我真的很难弄清楚在主题或其他轻量级附加组件中可以轻松添加的范围内，从哪里开始着手解决这个问题。

---

<div class="post-metadata">

**Author:** ![Don](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/don/32/228726_2.png) [@Don](https://meta.discourse.org/u/Don)\
**Post date:** [2024年三月28日 21:15 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390/2 "2024-03-28T21:15:23Z")

</div>

你好 👋

这使用了以下格式。

> <https://github.com/discourse/discourse/blob/fa92f8bc94deb5969caf6af6943b0cb3c6725fe5/app/assets/javascripts/discourse/app/lib/formatter.js#L41-L51>

* * *

你可以在 `/admin/customize/site_texts` 中更改它

搜索这两个字符串。

`dates.long_date_with_year`  
`dates.long_date_without_year`

* * *

**dates.long\_date\_with\_year**

默认  
`MMM D, YYYY LT`

更改为  
`MMM D, YYYY LTS`

* * *

**dates.long\_date\_without\_year**

默认  
`MMM D, LT`

更改为  
`MMM D, LTS`

* * *

> ⚠ 注意：这将更改你网站上所有使用此日期格式的地方。

---

<div class="post-metadata">

**Author:** ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)\
**Post date:** [2024年三月28日 21:47 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390/3 "2024-03-28T21:47:15Z")

</div>

为了完成 Don 的精彩回答，如果您只想在本次分享主题模态框中实现，可以尝试此组件：

> **[GitHub - Arkshine/discourse-share-topic-date](https://github.com/arkshine/discourse-share-topic-date)**
>
> Contribute to Arkshine/discourse-share-topic-date development by creating an account on GitHub.

**设置：**

 ![image](https://global.discourse-cdn.com/meta/original/4X/a/a/a/aaa478838bd32ecacd2ef4cc66c72027598f3f26.png)

**结果：**

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/1/5/b150dae9f046fb92654c220e09a8e6a091628cd7.png)

👍

---

<div class="post-metadata">

**Author:** ![orangeandblack5](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@orangeandblack5](https://meta.discourse.org/u/orangeandblack5)\
**Post date:** [2024年四月5日 01:09 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390/4 "2024-04-05T01:09:25Z")

</div>

这似乎太不可思议了

但是

当我们尝试安装它时，我们遇到了 500 内部服务器错误

您作为作者，是否有什么技巧可以告诉我该查找什么来解决这个问题？

我的假设是，因为我们正在运行一个一年前的 Discourse 稳定分支版本，而且您所做的某些事情可能与此不兼容（至少，假设它在您的生产环境中运行正常）。如果您能大致指出我需要查找哪些方面的差异，因为您似乎比我更熟悉这一切，我很乐意分叉存储库并自己进行回溯移植，而不是让您继续为我工作。

---

<div class="post-metadata">

**Author:** ![orangeandblack5](https://avatars.discourse-cdn.com/v4/letter/o/edb3f5/32.png) [@orangeandblack5](https://meta.discourse.org/u/orangeandblack5)\
**Post date:** [2024年四月5日 02:37 UTC](https://meta.discourse.org/t/how-to-make-post-timestamps-show-seconds/301390/5 "2024-04-05T02:37:59Z")

</div>

查看后，我认为 API 的工作方式可能有所不同。

我采用了这段原始代码：

```plaintext
import { apiInitializer } from "discourse/lib/api";
import discourseComputed from "discourse-common/utils/decorators";

function longDateNoYearWithSeconds(dt) {
  if (!dt) {
    return;
  }

  if (new Date().getFullYear() !== dt.getFullYear()) {
    return moment(dt).format(settings.share_topic_date_with_year_format);
  } else {
    return moment(dt).format(settings.share_topic_date_without_year_format);
  }
}

export default apiInitializer("1.8.0", (api) => {
  api.modifyClass("component:modal/share-topic", {
    pluginId: "share-topic",
    @discourseComputed("post.created_at", "post.wiki", "post.last_wiki_edit")
    displayDate(createdAt, wiki, lastWikiEdit) {
      const date = wiki && lastWikiEdit ? lastWikiEdit : createdAt;
      return longDateNoYearWithSeconds(new Date(date));
    },
  });
});

```

并根据我们看到的其他主题组件进行了更新：

```plaintext
import { withPluginApi } from "discourse/lib/plugin-api";
import discourseComputed, { on } from "discourse-common/utils/decorators";

function longDateNoYearWithSeconds(dt) {
  if (!dt) {
    return;
  }

  if (new Date().getFullYear() !== dt.getFullYear()) {
    return moment(dt).format(settings.share_topic_date_with_year_format);
  } else {
    return moment(dt).format(settings.share_topic_date_without_year_format);
  }
}

export default {
  name: "post-timestamp-mod",
  initialize(container) {
    withPluginApi("0.10.0", (api) => {
      api.modifyClass("component:modal/share-topic", {
        pluginId: "share-topic",
        @discourseComputed("post.created_at", "post.wiki", "post.last_wiki_edit")
        displayDate(createdAt, wiki, lastWikiEdit) {
          const date = wiki && lastWikiEdit ? lastWikiEdit : createdAt;
          return longDateNoYearWithSeconds(new Date(date));
        },
      });
    });
  }
}

```

它还没有起作用，但也许我最终会弄清楚？我可以在哪里查找来弄清楚所有这些 API 是做什么的？
