Home > Enterprise >  How can i separate this message in python?
How can i separate this message in python?

Time:06-12

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.

CodePudding user response:

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

CodePudding user response:

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])

CodePudding user response:

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
  • Related