7

I have a webhook that posts a message to a Slack channel. Is there a way to ensure that times mentioned in the message are shown in the user's local timezone?

Dan Ross
  • 3,596
  • 4
  • 31
  • 60
  • Do you mean that everyone who views the message would potentially see different text? I'm pretty sure the answer is no. – user94559 Jun 12 '16 at 09:04

4 Answers4

11

Slack's API allows you to output time in the user's timezone using a special placeholder. See Formatting Dates in the API documentation.

Rycieos
  • 43
  • 7
Brad Daily
  • 792
  • 8
  • 14
  • The docs on that page explicitly say *Messages containing a date or time should be displayed in the local timezone of the person seeing the message.*... – Daniel van Flymen Jan 06 '17 at 05:15
  • 1
    @DanielvanFlymen Not sure I follow your comment. The OP's question was how to display times differently per client depending on the end user's timezone. The above link shows you exactly how to do that. – Brad Daily Jan 07 '17 at 21:06
  • Link no longer working. – Looft Feb 07 '23 at 12:31
1

As per the slack API documentation, the chat.postMessage method returns a JSON with a response like this one here

{
    "ok": true,
    "ts": "1405895017.000506",
    "channel": "C024BE91L",
    "message": {
        ...
    }
}

So, i guess you can parse and get the timestamp and format it according to your need . Here is the link to slack api postMessage API documentation. Just to mention, tsis timestamp in above JSON response.

MatejMecka
  • 1,448
  • 2
  • 24
  • 37
Saurabh Chaturvedi
  • 2,028
  • 2
  • 18
  • 39
1

As described in https://api.slack.com/reference/surfaces/formatting#date-formatting you can use special date notation <!date^timestamp^token_string^optional_link|fallback_text> to have time displayed using viewers local TZ.

A snippet for GO that I use to format time for slack :

func SlackFormat(t time.Time) string {
    return `<!date^` + strconv.FormatInt(t.Unix(), 10) + `^{date_num} {time}|` + t.Format(time.RFC3339) + `>`
}
Radek 'Goblin' Pieczonka
  • 21,554
  • 7
  • 52
  • 48
0

Looks like it is impossible to display dates in the user's timezone in a shared channel. Even though the Slack clients for Linux and Android both seem to be using an embedded browser as a platform for their UI, there is no way that we can run JS on the client, which means that we can't do anything dynamic, like looking up the local TZ.

If the message were a direct message to a single user, then codehat's answer would work, but if we don't want timezones to differ between channel messages and direct messages.

Dan Ross
  • 3,596
  • 4
  • 31
  • 60