-1

I need to have the date. The format of the time should be 2017-01-18T07:34:35Z I have tried this but it does not work:

import datetime
i = datetime.datetime.now()
date = %i.year "-" %i.month "-" %i.day "T" %i.hour ":" %i.minute ":" %i.second "Z"
Christian König
  • 3,437
  • 16
  • 28
RUSLAN
  • 37
  • 1
  • 3
  • 1
    Please read the docs of `datetime`, especially the part about `strftime`. – ForceBru May 03 '17 at 14:20
  • 1
    It is actually really easy to get a time formatted how you want. There are countless examples on the net. I would just google (python date time) and see all the very useful examples many of them will also lead back to Q/A here on StackOverflow. please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask). – Mike - SMT May 03 '17 at 14:24

2 Answers2

5
from time import gmtime, strftime
strftime("%a, %d %b %Y %H:%M:%S", gmtime())

# Will output
'Thu, 03 May 2017 16:21:01'

for your example use:

from time import gmtime, strftime
strftime("%y-%d-%mT%H:%M:%SZ", gmtime())
2017-03-05T07:34:35Z
Ludisposed
  • 1,709
  • 4
  • 18
  • 38
3
    import time

    now = time.strftime("%c")

see more in https://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/

Pr3ds
  • 393
  • 3
  • 14