How to get the milliseconds of the beginning of today? - python

I can get current time in milliseconds as follows:
import time
timestamp = int(time.time()*1000.0)
However, how can I get the milliseconds of the beginning of today, e.g. 09/09/2018 00:00 ?

import pandas as pd
import datetime
int(pd.to_datetime(datetime.datetime.now().date()).value / 1000000)
# Outputs: 1536451200000

I hope this works to get the required start of the date's time in milliseconds in Python 3
from datetime import datetime
dt_obj = datetime.strptime('09.09.2019 00:00',
'%d.%m.%Y %H:%M')
millisec = dt_obj.timestamp() * 1000
print(millisec)
Output>> 1568001600000.0

Related

python datetime calculation not trimming to milliseconds

Doing the following calculation to take 30 days of the current date using the date-time module. The calculation is correct but it's not trimming milliseconds to 3 digits. any idea how I can implement it?
from datetime import datetime,timedelta
datetime_limit = datetime.today().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
datetime_limit = datetime.today() - timedelta(days=30)
The last digits are added when you do the calculation with timedelta.
from datetime import datetime,timedelta
datetime_limit = str(datetime.today() - timedelta(days=30))[:-3]
print(datetime_limit)

Convert local time to UNIX

I'm new to python and I'm trying to get the actual minutes passed every day since 7:00.
I am using mktime to get now_date1 and now_date2 in seconds, and then the plan it's to subtract and divide by 60 to get the minutes.
But I get the following error:
AttributeError: 'str' object has no attribute 'timetuple'
It's this the correct approach?
Here it's the code
import time
import pytz
from datetime import datetime
from time import mktime as mktime
now_date = datetime.now(pytz.timezone('Europe/Bucharest'))
now_date1 = now_date.strftime('%H:%M:%S')
now_date2 = now_date.strftime('7:00:00')
# Convert to Unix timestamp
d1_ts = time.mktime(now_date1.timetuple())
strftime returns a string. Not what you want.
You were pretty close, but there's no need to put time in the mix. Just modify your code like this and use time delta from datetime (inspired by How to calculate the time interval between two time strings):
import pytz
from datetime import datetime
now_date = datetime.now(pytz.timezone('Europe/Bucharest'))
from datetime import datetime
FMT = '%H:%M:%S'
now_date1 = now_date.strftime(FMT)
now_date2 = now_date.strftime('7:00:00')
tdelta = datetime.strptime(now_date1, FMT) - datetime.strptime(now_date2, FMT)
print(tdelta)
I get: 6:40:42 which seems to match since it's 12:42 here.
To get the result in minutes just do:
tdelta.seconds//60
(note that the dates have only correct hour/time/seconds, the year, month, etc.. are 1900 ... since they're not used)
I think something like this might work:
import time
import datetime
from time import mktime as mktime
#current time
now_date = datetime.datetime.now()
#time at 7am
today = datetime.date.today()
now_date2 = datetime.datetime(today.year, today.month, today.day, 7, 0, 0, 0)
#difference in minutes
(now_date - now_date2).days * 24 * 60

Converting between time formats

Is there a way to convert time from the year_month_day-hh_mm_ss to timestapm (in milliseconds since 1971) with DateUtils? or some other library..
thanks.
Have a look at the Python datetime and time modules.
from datetime import datetime
d = datetime.strptime("2017_03_16-14:08:10", "%Y_%m_%d-%H:%M:%S")
This will create a datetime object of d
Then use mktime from Python's time module to get your timestamp
import time
time.mktime(d.timetuple())*1000
The *1000 is required to convert from seconds to milliseconds.
Also, do you mean 1971 or the Unix epoch (Jan 01 1970)?
Try the arrow module found at the following URL: https://pypi.python.org/pypi/arrow
You can parse the time with strptime, then you can get the time since epoch time in milliseconds by using strftime to format only seconds. Multiply by 1000 to get milliseconds.
converted_time.strftime("%s") * 1000
You can use timedelta
from datetime import timedelta
year = timedelta(days=(2017-1971)*365)#number of days from 1971 to 2017
mili_sec = (year.total_seconds())*1000#you will get total_seconds just mulitply with 1000 to get milliseconds
OUTPUT
1450656000000.0
OR
You wanted difference from a particular date.Ex from 1971-01-01 to 2017-03-16-14:08:10
from datetime import datetime
new_day = datetime.strptime("2017_03_16-14:08:10", "%Y_%m_%d-%H:%M:%S")
old_day = datetime.strptime("1971_01_01-00:00:00", "%Y_%m_%d-%H:%M:%S")
diff_day_milliseconds = ((new_day - old_day).total_seconds())*1000
OUTPUT
1458137290000.0

having issues with python time object

i am having a small program that gets time from system and dose some arithemetic on it.
here is my code:
import time
import datetime
c_time = time.strftime("%H:%M;%S")
# now to convert it into time object i am using
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")
print c_time_converted
the output is of the form :
1900-01-01 11:39:40
but i want only the time part when i split and perform other operations on it i get a string returned but i need in time format so that i can perform time arethemetic
i want it in form 11: 39:40
Just use .time():
from datetime import datetime
c_time = '2015-06-08 23:13:57'
c_time_converted = datetime.strptime(c_time,"%Y-%m-%d %H:%M:%S")
print c_time_converted
print c_time_converted.time()
prints:
2015-06-08 23:13:57
23:13:57
EDIT (addressing a comment regarding datetime)
Here is a simple example of using a difference in datetime:
from datetime import timedelta
c_time_later = '2015-06-08 23:50:21'
c_time_later_converted = datetime.strptime(c_time_later,"%Y-%m-%d %H:%M:%S")
dt = c_time_later_converted - c_time_converted
print dt # time I was away from stackoverflow
prints: 0:36:24
now we will add dt time back to c_time above and see that we recover c_time_later:
print (c_time_converted + dt).time()
prints: 23:50:21
Or add an hour using timedelta:
print (c_time_converted + timedelta(hours=1)).time() # prints 00:13:57
I made a minor change in your code. Please check. it will give you time only.
import time
import datetime
c_time = time.strftime("%H:%M:%S")
# now to convert it into time object i am using
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M:%S")
print c_time_converted.time()
Try to this.
import time
import datetime
c_time = time.strftime("%H:%M;%S")
c_time_converted = datetime.datetime.strptime(c_time,"%H:%M;%S")
print c_time_converted.time()
Output :
12:04:31
Get Current Date and Time.
from datetime import datetime
print datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Output:
2015-06-09 12:09:09

Python: get datetime of last hour

I want to get the date time object for last hour.
Lets say the sys time is "2011-9-28 06:11:30"
I want to get the output as "2011-9-28 05" #{06 - 1 hour}
I used:
lastHourDateTime = date.today() - timedelta(hours = 1)
print lastHourDateTime.strftime('%Y-%m-%d %H:%M:%S')
However, my output is not showing the time part at all. where am I going wrong?
Date doesn't have the hour - use datetime:
from datetime import datetime, timedelta
last_hour_date_time = datetime.now() - timedelta(hours = 1)
print(last_hour_date_time.strftime('%Y-%m-%d %H:%M:%S'))
This works for me:
import datetime
lastHourDateTime = datetime.datetime.now() - datetime.timedelta(hours = 1)
print(lastHourDateTime.strftime('%Y-%m-%d %H'))
# prints "2011-09-28 12" which is the time one hour ago in Central Europe
You can achieve the same goal using pandas:
import pandas as pd
pd.Timestamp.now() - pd.Timedelta('1 hours')

Categories

Resources