0

I have a send_sms function, but the date format from the message is too long and detailed. I want it to format from 2021-01-24 05:12:21.517854+00:00 into just (5:12PM, 24Jan2021)

How can I do it? Thanks!

def send_sms(request):
    z = Test.objects.latest('timestamp')
    numbers = Mobile.objects.all()
    message = (f'Test ({z.timestamp})')
class Test(models.Model):
    timestamp = models.DateTimeField(auto_now_add=True)
Junely
  • 21
  • 6
  • `datetime.fromisoformat("2021-01-24 05:12:21.517854+00:00").strftime("%I:%M%p %d%b%Y")`? just replace the datetime string with your variable... – FObersteiner Jan 27 '21 at 09:24
  • Great, Thank you! what I did is `z.timestamp.strftime("%I:%M%p %d%b%Y")` – Junely Jan 27 '21 at 10:07
  • ah right, `z.timestamp` is a datetime object, then parsing to datetime is not required ;-) – FObersteiner Jan 27 '21 at 10:09
  • related: [Change datetime object back to string in python](https://stackoverflow.com/q/45251957/10197418) and [How do I turn a python datetime into a string, with readable format date?](https://stackoverflow.com/q/2158347/10197418) – FObersteiner Jan 27 '21 at 10:24

0 Answers0