This question already has an answer here:
pandas time shift from utc to local
(1 answer)
Closed 4 years ago.
This is my code :
t = pd.to_datetime(df['timestamp'], unit='ms')
df['Time'] = t.dt.strftime('%H:%M:%S')
df['Hour'] = t.dt.hour
df['ChatDate'] = t.dt.strftime('%d-%m-%Y')
df['ChatDate'] = df['ChatDate']
The 'Time' field in the dataframe is in UTC, how do I get in my local time (Asia/Kolkata) or any other local time?
Use DatetimeIndex.tz_localize and DatetimeIndex.tz_convert:
t.dt.tz_localize('utc').dt.tz_convert('Asia/Calcutta')
Related
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
This question already has answers here:
Python: How to convert datetime format? [duplicate]
(2 answers)
Closed 1 year ago.
Have dd-mm-yy in "date" column
05-01-15
need yyyy-mm-dd
2015-01-05
Solved with
df['date'] = pd.to_datetime(df.date, format='%d-%m-%y', errors='coerce')
Has it another solution?
from datetime import datetime
now = datetime.now()
date_time = now.strftime("%Y-%m-%d")
print("date and time:",date_time)
Or have a closer look at strftime documentation.
df['date'] = pd.to_datetime(df['date'])
Will transform your custom format to pandas recognized datetime, which you will be able to use for calculations.
This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 2 years ago.
I am Trying to get the current date
This is my code
import datetime
x = datetime.datetime.now()
print(x)
But I need Only Date not date and Time both
import datetime
x = datetime.datetime.now().strftime('%Y-%m-%d')
print(x)
This question already has answers here:
Getting today's date in YYYY-MM-DD in Python?
(12 answers)
Closed 6 years ago.
I have the below code which works out the date 6 months ago from todays date.
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
six_months = date.today() + relativedelta(months=-6)
However I would like six_months to be in "%Y%m%d" format.
Thank you for your help.
You're looking for strftime.
six_months.strftime('%Y%m%d')
This should do the job for you.
This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'