datetime to timestamp python-python datetime now

datetime to timestamp python-python datetime now :

Hello everyone, This post is related to datetime to timestamp ans also python datetime now.
before going to learn if you want to learn python for free then click on python tutorials point.
It is very simple to get a datetime object from a timestamp in Python. However the same can't be said the other way arounds. Just when I start to think “oh Python’s very easy with a nice API” the further you looks into the language and library you start to notice some crack as. python
EXAMPLE 01): datetime to timestamp python

datetime to timestamp python-python datetime now
fig 01) datetime to timestamp python-python datetime now


from datetime import datetime
timestamps = 1545730074
data_object = datetime.fromtimestamp(timestamps)
print("data_object =", data_object)
print("type(data_object) =", type(data_object))

output: data_object = 2020-06-28 08:47:53
type(data_object) = <class 'datetime.datetime'>

Example 02: Python datetime to timestamp

You can get timestamps from a datetime object using datatime.timestamp() methods.
from datetime import datetime
#which is current date and time
current= datetime.current()
timestamp = datetime.timestamp(current)
print("timestamp =", timestamp)



EXAMPLE 03) Get current timestamp
import time
time.time()import time
time.time()#
Convert datetimes to timestamps
import time
from datetime import datetime
d = datetime(2020, 6, 28, 0, 0)
unixtime = time.mktime(d.timetuple())
In Python 3.3+ you can be use timestamp() instead in:
datetime.datetime(2020, 6, 28, 0, 0).timestamp()

Convert timestamp to datetime format

import pytz
from datetime import datetime
utc_data = datetime.utcfromtimestamp(unixtime).replace(tzinfo=pytz.utc)
print(utc_dt.strftime("%d.%m.%Y"))
# using calendar
import calendar
calendar.timegm(dt.utctimetuple())
from datetime import datetime
data = datetime.utcnow()
timestamp = 1545730074
# python 3
data_object = datetime.fromtimestamp(timestamp)

python datetime now

Example: Current date in different formats

from datetime import date

today = date.today()

# dd/mm/YY format
m1 = today.strftime("%d/%m/%Y")
print("m1 =", m1)

# Textual month, day and year format 
m2 = today.strftime("%B %d, %Y")
print("m2 =", m2)

# mm/dd/y format
m3 = today.strftime("%m/%d/%y")
print("m3 =", m3)

# Month abbreviation, day and year format 
m4 = today.strftime("%b-%d-%Y")
print("m4 =", m4)
OUTPUT:
m1 = 28/06/2020
m2 = jun 28, 2020
m3 = 06/28/2020
m4 = jun-28-2020
Tags: datetime to timestamp python-python datetime now-python date time format
for similar kind of post click on pythonslearning and if you like this post then please comment and share.
BEST OF LUCK!!!
Previous
Next Post »

Featured Snippet

10 ways to iterate through a list in python

10 ways to iterate through a list in python The list is similar to array in other languages except for python, which provides the extra b...