Skip to content

Instantly share code, notes, and snippets.

@m-root
Last active September 20, 2018 14:55
Show Gist options
  • Save m-root/80458b824944ba96b24a146fc0f1b9fa to your computer and use it in GitHub Desktop.
Save m-root/80458b824944ba96b24a146fc0f1b9fa to your computer and use it in GitHub Desktop.
TIME CONVERSIONS WITH DATETIME
import datetime
datetime.datetime.utcnow().strftime('%s.%f')
#1526127635.078271
str(datetime.datetime.utcnow())
#2018-05-12 15:20:35.078341
datetime.strptime(str(datetime.utcnow()),"%Y-%m-%d %H:%M:%S.%f").strftime('%s.%f')
#'1526189163.717260'
print(type(datetime.strptime(str(datetime.utcnow()),"%Y-%m-%d %H:%M:%S.%f").strftime('%s.%f')))
<type 'string'>
float(datetime.strptime(str(datetime.utcnow()),"%Y-%m-%d %H:%M:%S.%f").strftime('%s.%f')
#1526189163.717260
print(type(float(datetime.strptime(str(datetime.utcnow()),"%Y-%m-%d %H:%M:%S.%f").strftime('%s.%f'))))
<type 'float'>
time.strftime('%Y-%m-%d %H:%M', time.strptime("2004-06-03T00:44:35.45456Z", "%Y-%m-%dT%H:%M:%S.%fZ"))
#'2018-06-20 20:39'
@m-root
Copy link
Author

m-root commented Sep 20, 2018

def date(datestr="2018-09-20 14:52:20.119349", format="%Y-%m-%d %H:%M:%S.%f"):
    from datetime import datetime
    if not datestr:
        return datetime.today().minute
    return datetime.strptime(datestr, format).minute

print(date())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment