How to convert current datetime into 13 digits Unix timestamp? [duplicate] - python

This question already has answers here:
How can I convert a datetime object to milliseconds since epoch (unix time) in Python?
(14 answers)
Closed 3 years ago.
I want to convert current datetime into Unix time stamp
My Code
import time
import datetime
d = datetime.datetime.now()
unixtime = time.mktime(d.timetuple())
print(unixtime)
My output:
1577098747.0
Expected Output:
1577098747123.0
Above code gives me timestamp upto 10 digits but I want it to be accurate till 13 digits.
Note: I don't want to convert it manually multiplying by 10**3 I want to capture accurate milliseconds.

do it like this
import time
import datetime
d = datetime.datetime.now()
unixtime = datetime.datetime.timestamp(d)*1000
print(unixtime)
or you just use time.time()

Related

How to get time in '2022-12-01T09:13:45Z' this format? [duplicate]

This question already has answers here:
Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero offset)
(13 answers)
Closed 3 months ago.
from datetime import datetime
import pytz
# local datetime to ISO Datetime
iso_date = datetime.now().replace(microsecond=0).isoformat()
print('ISO Datetime:', iso_date)
This doesn't give me the required format i want
2022-05-18T13:43:13
I wanted to get the time like '2022-12-01T09:13:45Z'
The time format that you want is known as Zulu time format, the following code changes UTC to Zulu format.
Example 1
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
print(now)
Output
#2022-12-01 10:07:06.552326+00:00
Example 2 (Hack)
import datetime
now = datetime.datetime.now(datetime.timezone.utc)
now = now.strftime('%Y-%m-%dT%H:%M:%S')+ now.strftime('.%f')[:4] + 'Z'
print(now)
Output
#2022-12-01T10:06:41.122Z
Hope this helps. Happy Coding :)
You can use datime's strftime function i.e.
current_datetime = datetime.now().replace(microsecond=0)
print(f'ISO Datetime: {current_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")}')

Converting Date and time to UNIX date format [duplicate]

This question already has answers here:
Convert a column of yyyy-mm-dd datetimes to linux time epoch in Python
(3 answers)
Closed 7 months ago.
I have been trying to convert the date and time given in following format "2021/12/04 11:10:00.000" to UNIX time.I have attached the sample of my program I tried. It does provide me the UNIX time but when I checked for the converted UNIX time it didn't gave me the correct UNIX time conversion.
for i in dx['date']:
year= i[0:4]
month = i[5:7]
day = i[8:10]
hr = i[11:13]
minute = i[14:16]
sec = i[17:19]
time = year+','+month+','+day+','+hr+','+minute+','+sec
print("Debug", time)
unixtime = datetime.datetime(int(year),int(month),int(day),int(hr),int(minute),int(sec)).timestamp()
Convert string datetime to datetime obj, and then retrieve timestamp.
from datetime import datetime
t = "2021/12/04 11:10:00.000"
dt = datetime.strptime(t, "%Y/%m/%d %H:%M:%S.%f")
dt.timestamp()
Output:
1638637800.0

How to convert HH:MM:SS to time.time() object in Python [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 2 years ago.
I need to convert HH:MM:SS format to time.time() object. Is there any way to do it?
Here's my HH:MM:SS format time:
a = '14:37:29'
I want to convert it to time.time() object such as:
a = 1600256249
Is this achievable? If it's not, what should I try? Hope you help.
To get UNIX time, you need to add a date. For example, you could combine your time string with today's date:
from datetime import datetime, timezone
s = '14:37:29'
today = datetime.today() # 2020-09-16
# make a datetime object with today's date
dt = datetime.combine(today, datetime.strptime(s, '%H:%M:%S').time())
# make sure it's in UTC (optional)
dt = dt.replace(tzinfo=timezone.utc)
# get the timestamp
ts = dt.timestamp()
print(ts)
# 1600267049.0
You could also set other time zones with this approach using dateutil or zoneinfo (Python 3.9+).
Is this achievable?
I would say no. a = '14:37:29' holds only hour-minute-second, whilst time.time() does return seconds since start of epoch i.e. you would also need to known day, month and year beside hour, minute, second, to create equivalent of what time.time() returns.

Add time with integer number in python [duplicate]

This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 3 years ago.
I want to get current time and add it with an integer of hours. Example now is 11.00pm, May 12, 2019. I want to add 3 hours more. So the result would be 2.00 am May 13, 2019. Please help me to datetime + hours(integer type)
import datetime
currentDT = datetime.datetime.now()
print('Now is: '+ str(currentDT))
hours = int(input()) #any hours you want
result = currentDT + hours #it will get the errors here
Use datetime.now to obtain the current time, and add a datetime.timedelta:
from datetime import datetime, timedelta
n_hours = 3
date = datetime.now() + timedelta(hours=n_hours)
print(datetime.now())
# 2019-05-12 19:16:51.651376
print(date)
# 2019-05-12 22:16:51.464890

convert date to number python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fetching datetime from float and vice versa in python
Like many people I switched from Matlab to Python. Matlab represents each date as a number. (Integers being days since 00-00-0000, and fractions being time in the day). I believe that python does the same except off a different start date 0-0-0001
I have been playing around with datetime, but cant seem to get away from datetime objects and timedeltas. I am sure this is dead simple, but how do I work with plain old numbers (floats)?
perhaps as a bit of context:
i bring in a date and time stamp and concatenate them to make one time value:
from datetime import datetime
date_object1 = datetime.strptime(date[1][0] + ' ' + date[1][1], '%Y-%m-%d %H:%M:%S')
date_object2 = datetime.strptime(date[2][0] + ' ' + date[2][1], '%Y-%m-%d %H:%M:%S')
Try this out:
import time
from datetime import datetime
t = datetime.now()
t1 = t.timetuple()
print time.mktime(t1)
It prints out a decimal representation of your date.
The datetime class provides two methods, datetime.timestamp() that gives you a float and datetime.fromtimestamp(timestamp) that does the reverse conversion. To get the timestamp corresponding to the current time you have the time.time() function.
Note that POSIX epoch is 1970-01-01.

Categories

Resources