Date to datetime with timezone - python

How would I convert
"2022-02-01" to something like
"2022-02-01T()" there are no brackets I'd like to convert each 30 minute interval from 2022-02-01 00:30:00 H:M:S and so forth.
I wanted to use something like
startDates = [{'start': {'date': '2022-02-01'}}] # Minimal Reproducible Example
timeZone = pytz.timezone('America/Vancouver')
for event in startDates:
newStartDateTime = datetime.strptime(event['start']['date'], '%Y-%m-%d' )
print(newStartDateTime)
datetime_ist = datetime.strptime(event['start']['date'], '%Y-%m-%d' ).replace(tzinfo=timeZone)
print("Date & Time in : ",datetime_ist.strftime('%Y-%m-%dT%H:%M:%S%z'))
Which does produce a time slot at the first H:M:S I'd to obtain 48 of these
2022-02-01 00:00:00
Date & Time in : 2022-02-01T00:00:00-0812

First, make your datetime object timezone aware, using py.timezone.localize(datetime). And then convert it using astimezone().
For the 30-minute increments, use datetime.timedelta to keep adding 30 mins to your newStartDateTime.
from datetime import datetime, timedelta
import pytz
vancouver_tz = pytz.timezone('America/Vancouver')
ist_tz = pytz.timezone('Asia/Calcutta')
startDates = [{'start': {'date': '2022-02-01'}}] # Minimal Reproducible Example
for event in startDates:
newStartDateTime = datetime.strptime(event['start']['date'], '%Y-%m-%d')
vancouver_time = vancouver_tz.localize(newStartDateTime)
india_time = vancouver_time.astimezone(tz=ist_tz)
print("first Date & Time in IST:", india_time.strftime('%Y-%m-%dT%H:%M:%S%z'))
# now add increasing timedeltas in minute chunks
for delta in range(0, 30 * 48, 30):
offsetted_ist = india_time + timedelta(minutes=delta)
print("Date & Time in IST:", offsetted_ist.strftime('%Y-%m-%dT%H:%M:%S%z'))
Output:
first Date & Time in IST: 2022-02-01T13:30:00+0530
Date & Time in IST: 2022-02-01T13:30:00+0530
Date & Time in IST: 2022-02-01T14:00:00+0530
Date & Time in IST: 2022-02-01T14:30:00+0530
...
Date & Time in IST: 2022-02-02T12:00:00+0530
Date & Time in IST: 2022-02-02T12:30:00+0530
Date & Time in IST: 2022-02-02T13:00:00+0530
Btw, choose if you want to use camelCase or lowercase_with_underscores for your variables. Pick one. Python is typically lowercase_with_underscores, except classes which are UpperCamelCase.

Related

how to get last month days from a period of datetime in python?

Hi I have a startDate and endDate in python, the goal here is to get the same day but previous month and if the endDate does not exist returns back last date of the month.
From this
startDate = '01-03-2022'
endDate= '31-03-2022'
to
prevStartDate ='01-02-2022'
prevEndDate = '28-02-2022'
where
from datetime import datetime
from dateutil.relativedelta import relativedelta
period = endDate-startDate
prevStartDate = startDate- relativedelta(months=1)
prevEndDate = endDate+ period
how to do eosmonth like in excel so that i can put the if condition for prevEndDate the last month? or if there's another way to approach this?

subtract one day from timestamp - python

'I'm trying to subtract a day from this date 1590074712 in order to make 1590008151 but can't figure out any way to achieve that.
I've tried with:
from datetime import datetime
ts= 1590074712
date = datetime.timestamp(ts) - timedelta(days = 1)
print(date)
How can I subtract a day from a date in the above format?
I want the output in timestamp
Use datetime.fromtimestamp():
from datetime import datetime, timedelta
ts= 1590074712
date = datetime.fromtimestamp(ts) - timedelta(days = 1)
print(date)
Prints:
2020-05-20 15:25:12

Set start date to beginning of the year and end date to one day prior to current date

I am trying to set startdate and enddate in python but ran across some problem. What i want is start date set to beginning of the year and enddate set to one day prior to current date.
from datetime import datetime, timedelta, time,date
my_str= '2020-01-01'
stdate= datetime.strptime(my_str,'%Y-%m-%d')
print(stdate)
edate = datetime.now() - timedelta(days = 1)
print(edate)
Right now the output is date with time.But i only want to output date not time
2020-01-01 00:00:00
2020-03-01 12:25:50.542813
from datetime import date, timedelta
start_of_current_year = date(date.today().year, 1, 1)
end_of_previous_year = start_of_current_year - timedelta(days=1)
# Check values
print(start_of_current_year)
print(end_of_previous_year)

How to add a minute in Python to Hive timestamp format

I wanted to add a minute to the date which is in the Hive timestamp format of 'YYYY-MM-DD HH24:MI:SS.FF6' using Python
Example:1) 2015-01-15 08:59:16 date after adding a minute should become 2015-01-15 09:00:16
2)
For this, I got the minute part from the date and added 1 to it with a modulo 60:
str((int(csvRowArray[1][10:12])+ 1)%60)
Here csvRowArray[1] is the date in string format.
The problem comes when the minute is 59, although it resets the minute part to 0, the hour part doesn't increase. This should also work when the hour is 23:59 and we add a minute, the date should change.Or if the date is at the end of the month and the hour is 23:59 then even the month should change!
Is there a function in Python to read the date in Hive timestamp format and add a minute to it?
It will help if you can convert the object into DateTime instances as that will allow you to manipulate the time directly using timedelta. Taking your example of '2015-01-15 08:59:16'
from datetime import datetime as dt
from datetime import timedelta
a='2015-01-15 08:59:16'
dt_obj = dt.strptime(a, '%Y-%m-%d %H:%M:%S')
dt_with_one_more_min = dt_obj + timedelta(minutes=1)
print('original time = {}'.format(dt_obj))
print('new time = {}'.format(dt_with_one_more_min))
This will give the following result:
original time = 2015-01-15 08:59:16
new time = 2015-01-15 09:00:16
My example is with minutes, but you can try other modifications too with timedelta and it will work.

Python Get Date In Future (x) Days, And Hours Left To Date?

How can I get date + time in the future from now by days? in this format: 10/08/2013 9:50 PM (dd/mm/yyyy) and also I would like Time left to date in hours for this new future date?
You need to use a datetime in combination with a timedelta object.
Like this:
from datetime import datetime, timedelta
dt = datetime.now()
td = timedelta(days=4)
# your calculated date
my_date = dt + td

Categories