python get current time

now_time.py


from datetime import datetime
datetime.now().strftime("%Y/%m/%d %H:%M:%S")

Get datetime object for current date and time

from datetime import datetime

command result
datetime.now() datetime.datetime(2014, 1, 2, 3, 4, 5, 000000)

Add datetime object

from datetime import datetime, timedelta

command result
datetime(2014,1,2,3,4,5) datetime.datetime(2014, 1, 2, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(weeks=1) datetime.datetime(2014, 1, 9, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(days=1) datetime.datetime(2014, 1, 3, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(hours=1) datetime.datetime(2014, 1, 2, 4, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(minutes=1) datetime.datetime(2014, 1, 2, 3, 5, 5)
datetime(2014,1,2,3,4,5) + timedelta(seconds=1) datetime.datetime(2014, 1, 2, 3, 4, 6)
datetime(2014,1,2,3,4,5) + timedelta(milliseconds=1) datetime.datetime(2014, 1, 2, 3, 4, 5, 1000)
datetime(2014,1,2,3,4,5) + timedelta(microseconds=1) datetime.datetime(2014, 1, 2, 3, 4, 5, 1)

Subtract the datetime object

from datetime import datetime, timedelta

command result
datetime(2014,1,2,3,4,5) datetime.datetime(2014, 1, 2, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(weeks=-1) datetime.datetime(2013, 12, 26, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(days=-1) datetime.datetime(2014, 1, 1, 3, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(hours=-1) datetime.datetime(2014, 1, 2, 2, 4, 5)
datetime(2014,1,2,3,4,5) + timedelta(minutes=-1) datetime.datetime(2014, 1, 2, 3, 3, 5)
datetime(2014,1,2,3,4,5) + timedelta(seconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4)
datetime(2014,1,2,3,4,5) + timedelta(milliseconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4, 999000)
datetime(2014,1,2,3,4,5) + timedelta(microseconds=-1) datetime.datetime(2014, 1, 2, 3, 4, 4, 999999)

datetime object → number

from datetime import datetime

command result
datetime(2014,1,2,3,4,5).year 2014
datetime(2014,1,2,3,4,5).month 1
datetime(2014,1,2,3,4,5).day 2
datetime(2014,1,2,3,4,5).hour 3
datetime(2014,1,2,3,4,5).minute 4
datetime(2014,1,2,3,4,5).second 5

datetime object → string

from datetime import datetime

command result Supplement
datetime(2014,1,2,3,4,5).strftime('%Y/%m/%d %H:%M:%S') 2014/01/02 03:04:05
datetime(2014,1,2,3,4,5).strftime('%Y') 2014 Year
datetime(2014,1,2,3,4,5).strftime('%y') 14 Year(Last 2 digits)
datetime(2014,1,2,3,4,5).strftime('%m') 01 Month
datetime(2014,1,2,3,4,5).strftime('%d') 02 Day
datetime(2014,1,2,3,4,5).strftime('%H') 03 Time(24-hour notation)
datetime(2014,1,2,3,4,5).strftime('%I') 03 Time(12 hour notation)
datetime(2014,1,2,3,4,5).strftime('%M') 04 Minutes
datetime(2014,1,2,3,4,5).strftime('%S') 05 Seconds
datetime(2014,1,2,3,4,5).strftime('%b') Jan Short month name
datetime(2014,1,2,3,4,5).strftime('%B') January Month name
datetime(2014,1,2,3,4,5).strftime('%a') Thu Short day name
datetime(2014,1,2,3,4,5).strftime('%A') Thursday Day of the week name
datetime(2014,1,2,3,4,5).strftime('%p') AM AM or PM
datetime(2014,1,2,3,4,5).isoformat() 2014-01-02T03:04:05 ISO 8601 format
datetime(2014,1,2,3,4,5).ctime() Thu Jan 2 03:04:05 2014 %a %b %d %X %Y
datetime(2014,1,2,3,4,5).strftime('%c') Thu Jan 2 03:04:05 2014 %a %b %d %X %Y
datetime(2014,1,2,3,4,5).strftime('%x') 01/02/14 %m/%d/%y
datetime(2014,1,2,3,4,5).strftime('%X') 03:04:05 %H:%M:%S
datetime(2014,1,2,3,4,5).strftime('%s') 1388599445 unixtime

String → datetime object

from datetime import datetime

command result
datetime.strptime('2014-01-02 03:04:05', '%Y-%m-%d %H:%M:%S') datetime.datetime(2014, 1, 2, 4, 3, 5)

datetime object → unixtime

from datetime import datetime

command result
datetime(2014,1,2,3,4,5).strftime('%s') 1388599445

unixtime → datetime object

from datetime import datetime

command result
datetime.fromtimestamp(1388599445) datetime.datetime(2014, 1, 2, 4, 3, 5)

datetime object → date object

from datetime import datetime

command result
datetime(2014,1,2,3,4,5).date() datetime.date(2014, 1, 2)

datetime object → time object

from datetime import datetime

command result
datetime(2014,1,2,3,4,5).time() datetime.time(3, 4, 5)

Recommended Posts

python get current time
Python Note: Get the current month
First time python
python time measurement
First time python
Get the current date and time in Python, considering the time difference
Get time series data from k-db.com in Python
Python: Time Series Analysis
[Python] Get environment variables
Function execution time (Python)
[Python] Get Qiita trends
Python time series question
Output python execution time
Time floor function (Python)
Get and convert the current time in the system local timezone with python
[Python3] Get date diff
Get date in Python
Get date with python
Extract "current date only" and "current date and time" with python datetime.
Get standard output in real time with Python subprocess
Get your current location and user agent in Python
Get YouTube Comments in Python
Get country code with python
Python --bitflyer Get asset balance
Get last month in python
Get Twitter timeline with python
Get Youtube data with python
Get Terminal size in Python
[Python] Get the previous month
Explicitly get EOF in python
Execution time measurement with Python With
Get thread ID with python
[Python] Plot time series data
Get started with Python! ~ ② Grammar ~
[python] Get quotient and remainder
Get stock price with Python
Get Evernote notes in Python
python> Get substring> print serial [5:10] / print serial [5: -1]
IfcOpenShell python bindings get started
Time synchronization (Windows) with Python
(Python) Get AWS billing amount
Python --bitflyer Get chat content
Get Alembic information with Python
Python> dictionary> get ()> optional value
Get Japanese synonyms in Python
Get Leap Motion data in Python.
[Python] Check the current directory, move the directory
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Python: Time Series Analysis: Preprocessing Time Series Data
Get negative reaction time with psychopy.event.getKeys ()
Get data from Quandl in Python
Get reviews with python googlemap api
Get the desktop path in Python
Get the weather with Python requests
Get web screen capture with python
Get the weather with Python requests 2
[Python] Get economic data with DataReader
Get the script path in Python
Measure function execution time in Python
How to get the Python version