1

How can i sperate this message/output? I tried doing some research but I still couldn't fix it. This is my code:

@client.command()
async def userjoindate(ctx, username):
    user = await roblox.get_user_by_username(username)
    response = requests.get(f'https://users.roblox.com/v1/users/{user.id}')
    json_data = json.loads(response.text)
    message = json_data['created']
    await ctx.send(message)

The message/output is usually like this: 2012-06-28T17:54:30.74Z I just want it to be 2012-06-28 without the other part.

Ocryol
  • 61
  • 8
  • Does this answer your question? [How to display the first few characters of a string in Python?](https://stackoverflow.com/questions/11714859/how-to-display-the-first-few-characters-of-a-string-in-python) – TheFungusAmongUs Jun 11 '22 at 14:01

3 Answers3

1

Would you be able to get the first part of the date string?

message = '2012-06-28T17:54:30.74Z'
print(str(message)[0:10])

Output:

2012-06-28
Gold79
  • 344
  • 3
  • 9
0

You can do it both ways

One using dateutil parser

from dateutil import parser
formattedDate = parser.parse("2012-06-28T17:54:30.74Z").strftime("%Y-%m-%d")
print(x)

and one using split (less recommended)

date = "2012-06-28T17:54:30.74Z"
print(date.split("T")[0])
R C N
  • 85
  • 5
0

a clear way to do it is using regex module:

Code:

import re                                       # new import line
user = await roblox.get_user_by_username(username)
response = requests.get(f'https://users.roblox.com/v1/users/{user.id}')
json_data = json.loads(response.text)
message = json_data['created']
await ctx.send(message)
newout = re.findall("(\d+-\d+-\d+)", oldout)[0]  #new code line

Output:

 2012-06-28
Leonardo Scotti
  • 1,069
  • 8
  • 21