Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

datetime python

from datetime import datetime as d
date = d.now()
print(date.strftime("%Y-%m-%d %H:%M:%S"))
Comment

python datetime module

import datetime as d
time = d.datetime.now()
time = time.strftime("%Y-%m-%d  %H:%M:%S")
           #year-#month-#date  #hour:#minure:#second
print(time)
Comment

Python datetime object

from datetime import datetime

#datetime(year, month, day)
a = datetime(2018, 11, 28)
print(a)

# datetime(year, month, day, hour, minute, second, microsecond)
b = datetime(2017, 11, 28, 23, 55, 59, 342380)
print(b)
Comment

python datetime

from datetime import date
f_date = date(2014, 7, 2)
l_date = date(2014, 7, 11)
delta = l_date - f_date
print(delta.days)

Comment

python datetime

from datetime import datetime as dt
# from string
datetime = dt.strptime('2022-01-01 23:59:59', '%Y-%m-%d %H:%M:%S')

# to string
print(datetime.strftime("%Y-%m-%d %H:%M:%S"))
Comment

python date time

>>> import pytz
>>> from datetime import datetime, date, time
# To get the UTC time of current day (today) for a given time zone
# use the localize() function of the pytz. 
# The reason for this is that the localize() takes into account the Day Time Saving
# which niether the datetime constructors nor replace() account for.
# 
# To for example, the utc time of today in australia time zone:

import pytz
from datetime import datetime, date, time

# set time zone
tz = pytz.timezone("Australia/Melbourne")

# Get today's date in the current time zone (i.e local time)
todaydate = date.today() # you can also use date(2022, 2, 8)

# Get midnight of today (still local time)
# note time() without arguments will result to midnight

midnight_local = datetime.combine(todaydate, time())

print midnight_local

# convert this midnight datetime to the midnight datetime
# in the given time zone. In this case autralia time zone
austra_midnight = tz.localize(midnight_local)

print austra_midnight

# convert the midnight time in australia time zone to UTC
midnight_austra_utc = austra_midnight.astimezone(pytz.uct)

print midnight_austra_utc



# Ref:
# https://stackoverflow.com/questions/373370/how-do-i-get-the-utc-time-of-midnight-for-a-given-timezone
Comment

Python DateTime Class Syntax

class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
Comment

PREVIOUS NEXT
Code Example
Python :: selenium upload file python 
Python :: python regex to match ip address 
Python :: python extract all numbers from string re 
Python :: pandas groupby without reset index 
Python :: firebase python upload storage 
Python :: python record screen 
Python :: Right click context menu of a file in Python 
Python :: tensorflow binary cross entropy loss 
Python :: numpy distance between two points 
Python :: python how to obfuscate code 
Python :: pyspark correlation between multiple columns 
Python :: ctx.save_for_backward 
Python :: edit line if str end with pandas 
Python :: how to convert a dense matrix into sparse matrix in python 
Python :: Date difference in minutes in Python 
Python :: pandas add a column with loc 
Python :: csv python write 
Python :: python zip lists into dictionary 
Python :: t.interval scipy 
Python :: scikit learn ridge regression 
Python :: urllib.error.HTTPError: HTTP Error 403: Forbidden 
Python :: python cache return value 
Python :: python console command 
Python :: native bold text 
Python :: python template generics 
Python :: pandas normalize df 
Python :: how to get chat first name in telebot 
Python :: revesing case python 
Python :: how to visualize decision tree in python 
Python :: python transfer file 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =