Printing month and date using python - python

I am trying to print only the month and date in python as following :
09-December
08-October
How could I do that?

Try this
import datetime
now = datetime.datetime.now()
print now.strftime("%d-%B")
For more information on this : strftime

from datetime import datetime
dt = datetime.strptime("09/12/16", "%d/%m/%y")
dt.strftime("%d-%B")

from datetime import date
d = date(2016, 12, 9)
d.strftime("%d - %A")
# 9 - Friday
# month day year
#d.strftime("%A %d %B %Y")

Related

Subtracting days from variable in date and time

I need to subtract 1 day from the current date and time? How would i do that?
Here is my code:
from datetime import datetime
now = datetime.now()
date = (now.strftime("%Y-%m-%d %H:%M:%S"))
print(date)
Say the date is (2021-10-3) i need the time variable to be set to something like (2021-10-2) Changing the day by -1 day!
Use timedelta.
from datetime import datetime, timedelta
now = datetime.now()
yesterday = now - timedelta(days=1)
date = (yesterday.strftime("%Y-%m-%d %H:%M:%S"))
print(date)
Find more about timedelta here

How to convert a 'day/month' string to date in python and compare it with an Odoo date field?

I have a day/month string, I want to convert that string to date object and compare the last day of that month to another date
Example:
For 08/2021 (august, 2021) I want to compare the last day of that month (31-08-2021) to another date (date field),
For 02/2020 I what to compare 29-02-2020 < another_date (date field)
For 02/2021 I what to compare 28-02-2020 < another_date (date field)
You can use calendar.monthrange to find the last day in the month if you don't want to add dateutil.
import calendar
from datetime import datetime
def get_last_day_date(year_month_str):
date = datetime.strptime(year_month_str, "%m/%Y")
last_day = calendar.monthrange(date.year, date.month)[1]
return datetime(date.year, date.month, last_day)
get_last_day_date("08/2020")
# datetime.datetime(2020, 8, 31, 0, 0)
This examples shows you how to convert '02/2020' to a Python datetime and how to get the last day of that month. You can use it to compare the result to another datetime:
import datetime
from dateutil.relativedelta import relativedelta
date = '02/2020'
last_day = datetime.datetime.strptime(date, '%m/%Y') + relativedelta(day=31)
# last_day will be a datetime of the last day of the month which you can use to compare against another datetime
In this example, the result is datetime.datetime(2020, 2, 29, 0, 0) because 2020 was a leap year
b='08/2021'
a=b.split('/')
import calendar
import datetime
z=(str(calendar.monthrange(int(a[1]),int(a[0]))[1])+'-'+b.replace('/','-'))
d=datetime.datetime.strptime(z,'%d-%m-%Y').date()
print(d)
n=datetime.date.today()
print(n)
n<d
Output:
2021-08-31
2021-01-28
True
It can be done by just importing/using datetime library and here you can see how.
By passing string date into method.
import datetime
def convert_string_to_datetime(self, datetime_in_string):
datetime_in_string = str(datetime_in_string)
datetime_format = "%Y-%m-%d %H:%M:%S"
datetime_in_datetime_format = datetime.datetime.strptime(datetime_in_string, datetime_format)
return datetime_in_datetime_format
new_datetime_field = convert_string_to_datetime(datetime_in_string)
By modifying with in single line
import datetime
new_datetime_field = datetime.datetime.strptime(YOUR_DATETIME_IN_STRING, "%Y-%m-%d %H:%M:%S")
After converting into datetime now comparison is possible like.
if new_datetime_field > odoo_datetime_field:
pass

How to fix date formatting using python3

I have data with the date format as follows:
date_format = 190410
year = 19
month = 04
date = 10
I want to change the date format, to be like this:
date_format = 10-04-2019
How do I solve this problem?
>>> import datetime
>>> date = 190410
>>> datetime.datetime.strptime(str(date), "%y%m%d").strftime("%d-%m-%Y")
'10-04-2019'
datetime.strptime() takes a data string and a format, and turns that into datetime object, and datetime objects have a method called strftime that turns datetime objects to string with given format. You can look what %y %m %d %Y are from here.
This is what you want(Notice that you have to change your format)
import datetime
date_format = '2019-04-10'
date_time_obj = datetime.datetime.strptime(date_format, '%Y-%m-%d')
print(date_time_obj)
Here is an other example
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
You can also do this
from datetime import datetime, timedelta
s = "20120213"
# you could also import date instead of datetime and use that.
date = datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print(date)
There are many ways to achieve what you want.

Get year,month and day from python variable

I'd like to get the break of a variable by year, month and day. Here's what I got:
import datetime
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
print (yesterday)
year = datetime.date.yesterday.year
month = datetime.date.yesterday.month
day=datetime.date.yesterday.day
print (year)
print (month)
print (day)
I'm getting an error that datetime.date has no attribute. I'm a total noob at python and I'm stuck, any help is appreciated
you were close
import datetime
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
print (yesterday)
year = yesterday.year
month = yesterday.month
day=yesterday.day
print (year)
print (month)
print (day)
result is
2019-03-10
2019
3
10
You can use strftime method
A simple example:
>>> from datetime import datetime
>>> now = datetime.utcnow()
>>> year_month_day_format = '%Y-%m-%d'
>>> now.strftime(year_month_day_format)
'2020-11-06'
>>> hour_minute_format = '%H:%M'
>>> now.strftime(hour_minute_format)
'22:54'
Hopping, it will help someones
You can also simplify your import statements like so:
from datetime import datetime, timedelta
yesterday = datetime.today() - timedelta(1)
print(yesterday)
year = yesterday.year
month = yesterday.month
day = yesterday.day
print(year)
print(month)
print(day)
You will get the output:
2019-03-10 21:19:36.695577
2019
3
10
For current day
import datetime
current_datetime=datetime.datetime.now()
print("current_year:{}".format(current_datetime.year))
print("current_month:{}".format(current_datetime.month))
print("current_day:{}".format(current_datetime.day))
If you want in this format for example "10-Oct-2018". You can try this code for current day.
from datetime import datetime, timezone
now_utc = datetime.now(timezone.utc)
year = now_utc.strftime("%Y")
month = now_utc.strftime("%b")
day = now_utc.strftime("%d")
result = day+"-"+month+"-"+year
print(result)

How to increment an integer day by 5 days in python

Very new to python, please excuse the noob question:
I have a number that represents a date like :
date = 20121228
( representing December 28th, 2012)
How can I increment that date by 5 days in python so I end up with a new (correct) number representing the date like
date = 20130102
I don't want:
date = 20121233
Update: When I try and use datetime.strptime I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
Try this:
date = 20121228
from datetime import datetime, timedelta
dt = datetime.strptime(str(date), "%Y%m%d").date() + timedelta(days=5)
print datetime.strftime(dt, "%Y%m%d")
Parse it to a datetime.date() object, add 5 days, then reformat back to your number:
from datetime import datetime, timedelta
dt = datetime.strptime(str(date), '%Y%m%d').date()
dt += timedelta(days=5)
date = int(dt.strftime('%Y%m%d'))
Demonstration:
>>> from datetime import datetime, timedelta
>>> date = 20121228
>>> dt = datetime.strptime(str(date), '%Y%m%d').date()
>>> dt += timedelta(days=5)
>>> int(dt.strftime('%Y%m%d'))
20130102
For Python versions before Python 2.5, you'll need to use the time.strptime() version:
import time
from datetime import datetime, timedelta
dt = datetime(*(time.strptime(str(date), '%Y%m%d')[:6]))
dt += timedelta(days=5)
date = int(dt.strftime('%Y%m%d'))
You'll need to convert your date into a string first. Then use a timedelta object to add the days to the datetime object.
from datetime import datetime, timedelta
d = datetime.strptime(str(20121228), "%Y%m%d")
print (d + timedelta(days=5)).strftime("%Y%m%d")

Categories

Resources