1

I am trying to make my own bot right now. Now I am facing a small problem... When receiving messages via API the JSON looks like this:

   {
  "update_id": 297528782,
  "message": {
    "message_id": 2,
    "from": {
      "id": 148361373,
      "is_bot": false,
      "first_name": "yz",
      "username": "yz"
    },
    "chat": {
      "id": -403635451,
      "title": "bottest",
      "type": "group",
      "all_members_are_administrators": false
    },
    "date": 1583942557,
    "left_chat_participant": {
      "id": 1138406331,
      "is_bot": true,
      "first_name": "xxxxxxxxxbot",
      "username": "xxxxxxxxxbot"
    },
    "left_chat_member": {
      "id": 1138406331,
      "is_bot": true,
      "first_name": "xxxxxxxxxbot",
      "username": "xxxxxxxxxbot"
    }
  }
}

How can I transform something like ("date":1583942557) to something like dd-MM-YYYThh:mm?

Thanks in advance

dan1st
  • 12,568
  • 8
  • 34
  • 67
calif
  • 11
  • 1
  • 2
  • Does this answer your question? [Converting Long to Date in Java returns 1970](https://stackoverflow.com/questions/7487460/converting-long-to-date-in-java-returns-1970) – Ammar Hussein Mar 11 '20 at 16:39
  • Now that the previous comment is mentioning Java: are you using a programming language for that? – Ole V.V. Mar 22 '20 at 08:36

1 Answers1

4

Telegram creates timestamp of the message as a UNIX-time (seconds since Jan 01 1970)

To convert it to 'YYYY-MM-DD HH:MM:SS' string you can use the following:

import datetime, time

data = json.load('json_file_with_telegram_message.json')

messageTime = data['message']['date'] # UNIX time
messageTime = datetime.datetime.utcfromtimestamp(messageTime) # datetime format
messageTime = messageTime.strftime('%Y-%m-%d %H:%M:%S') # formatted datetime
    
TimeStamp = str(messageTime)

Enjoy!