How to make post timestamps show seconds?

Looked around for this but couldn’t find anything - what would be the simplest way to show the time a post was made down to the second in the share popup?

image

Each post already has the exact timestamp available in the HTML, so this information is certainly present, but I’m not 100% sure what the best way to update this text (which, sadly, seems to be hardcoded, not a formatted date) to add on the seconds would be. I’m mostly used to CSS and not so much JavaScript (I can read and understand it well enough, but am not well-versed enough to know the “proper” way to even begin to attempt solving this [most of my coding experience is embedded]), so I’m really struggling to figure out where to even start approaching this within the context of what could be easily added in a theme or similar lightweight add-on.

Hello :wave:

This uses the following formats.


You can change it in /admin/customize/site_texts

Search these two strings.

dates.long_date_with_year
dates.long_date_without_year


dates.long_date_with_year

Default
MMM D, YYYY LT

Change it to
MMM D, YYYY LTS


dates.long_date_without_year

Default
MMM D, LT

Change it to
MMM D, LTS


:warning: Note: This will change everywhere on your site where this date format used.

9 Likes

To complete the nice Don’s answer, if you want only on this share-topic modal, you can try this component:

Settings:

image

Result:

image

:+1:

6 Likes

Okay so this seems incredible

But

We get a 500 Internal Server Error when we try to install it

Don’t suppose you, as the author, have any tips as to what I should be looking for to resolve this?

My assumption is that it’s because we’re running a year-old stable-branch version of Discourse, and likely something you’ve done doesn’t play nice with that (at least, assuming it works for you on the live build). If you can point me in the vaguely correct direction as to what I would need to look for in terms of differences, since you seem way more familiar with all of this than I am, I’d be more than happy to fork the repo and do the actual work to backport it myself rather than making you continue to work for me.

Taking a look, it seems to me that the API might work differently

I’ve taken this original code

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));
    },
  });
});

and updated it to this:

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));
        },
      });
    });
  }
}

based on what I’m seeing in other theme components we have

It doesn’t yet work but maybe I’ll figure that out eventually? Where would I look to figure out what all this API stuff is doing?