Posts: 27
Threads: 8
Joined: Jan 2020
function current_date to return today's month, day, and year
def c_date():
pass
#TODO return month, day, year
m, d, y = c_date() [b]#How do I pass the date into c_date function here?[/b]
print("Today's date is: {:2d}/{:2d}/{:4d}".format(m, d, y)) Quote:Tried the as below it did not work
from datetime import date
dt = date.today()
m = dt.month()
print(m)
#def c_dt():
# dt = date.today()
# m = dt.month()
# d = dt[1:2]
# y = dt[:-1]
#c_dt()
#print("Today's date is: {:2d}/{:2d}/{:4d}".format(m, d, y))
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jan-27-2020, 08:34 AM
(This post was last modified: Jan-27-2020, 08:34 AM by perfringo.)
To get year, month and day from datetime.date one can just:
>>> date.today().year
2020
>>> date.today().month
1
>>> date.today().day
27 However, date can't be unpacked directly, because it's not iterable:
>>> year, month, day = date.today()
/.../
TypeError: cannot unpack non-iterable datetime.date object However, there is method .timetuple which can be used for unpacking. As it returns more values than we need one could use *:
>>> year, month, day, *_ = date.today().timetuple()
>>> year
2020
>>> month
1
>>> day
27
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 1,032
Threads: 16
Joined: Dec 2016
Jan-27-2020, 08:47 AM
(This post was last modified: Jan-27-2020, 08:47 AM by Axel_Erfurt.)
To get my local date I use
import locale
import datetime
loc = locale.getlocale()
locale.setlocale(locale.LC_ALL, loc)
dt = datetime.date.today().strftime("%-d.%B %Y")
dt2 = datetime.date.today().strftime("%-d.%m.%Y")
print(dt)
print(dt2) Output: 27.Januar 2020
27.01.2020
Posts: 2,127
Threads: 11
Joined: May 2017
(Jan-27-2020, 08:47 AM)Axel_Erfurt Wrote: To get my local date I use
Local language, but not local timezone.
To handle timezone without headache: pendulum
Before I used pytz, but the headache-factor is bigger.
You can do this also with the datetime-module, but it's not easy to handle it right.
Posts: 1,032
Threads: 16
Joined: Dec 2016
is this better?
import locale
import datetime
from dateutil.tz import tzlocal
loc = locale.getlocale()
locale.setlocale(locale.LC_ALL, loc)
print("LOCALE:", loc)
ltz = datetime.datetime.now(datetime.timezone(datetime.timedelta(0))).astimezone().tzinfo
print("LOCAL TIMEZONE:", ltz)
local = tzlocal()
now = datetime.datetime.now(tz=ltz)
now = now.replace(tzinfo = local)
print(now)
print(now.strftime("%-d.%B %Y %H:%M"))
print(now.strftime("%-d.%m.%Y %H:%M")) Output: LOCALE: ('de_DE', 'UTF-8')
LOCAL TIMEZONE: CET
2020-01-27 13:15:04.439200+01:00
27.Januar 2020 13:15
27.01.2020 13:15
Posts: 27
Threads: 8
Joined: Jan 2020
def cdt():
from datetime import date
y = date.today().year
m = date.today().month
d = date.today().day
return y,m,d
cdt()
print("Today's date is: {:2d}/{:2d}/{:4d}".format(m, d, y)) Getting this error
print("Today's date is: {:2d}/{:2d}/{:4d}".format(m, d, y))
NameError: name 'm' is not defined
####################################################################
How do I create this type of function
def c_date():
from datetime import date
y = date.today().year
m = date.today().month
d = date.today().day
return y,m,d
m, d, y = c_date()
#####################################################
# How do I use these m,d,y while calling the function ???
######################################################
Thanks u all
Posts: 1,950
Threads: 8
Joined: Jun 2018
Jan-28-2020, 05:57 AM
(This post was last modified: Jan-28-2020, 05:58 AM by perfringo.)
Without assigning names to what function returns there is nothing to refer in print statement. y, m, d are not visible outside the function. You should assign names if you call the function to use in print statement:
>>> y, m, d = cdt()
>>> print("Today's date is: {:02d}/{:02d}/{:4d}".format(d, m, y)) # note zero in day and month formatting
Today's date is: 28/01/2020 You can change the order of returning values and unpack it while printing (without assigning to names):
# in function change 'return y, m, d' --> 'return d, m, y'
>>> print("Today's date is: {:02d}/{:02d}/{:4d}".format(*cdt()))
Today's date is: 28/01/2020
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 27
Threads: 8
Joined: Jan 2020
Posts: 1,032
Threads: 16
Joined: Dec 2016
shorter
from datetime import date
def cdt():
d = date.today()
return d.strftime("%m/%d/%Y")
td = cdt()
print(f"Today's date is: {td}") Output: Today's date is: 01/28/2020
Posts: 7,320
Threads: 123
Joined: Sep 2016
Jan-28-2020, 06:20 PM
(This post was last modified: Jan-28-2020, 06:21 PM by snippsat.)
(Jan-28-2020, 11:10 AM)Axel_Erfurt Wrote: shorter Can also drop strftime and let f-string do all the job
>>> from datetime import date
>>>
>>> print(f"Today's date is: {date.today():%m/%d/%Y}")
Today's date is: 01/28/2020 (Jan-28-2020, 11:10 AM)Axel_Erfurt Wrote: is this better? Yes,but Pendulum as mention bye @ DeaD_EyE dos this better.
Can trow away a combination of datetime, dateutil, pytz ..ect,and use pendulum that dos Timezone correct.
>>> import pendulum
>>>
>>> now = pendulum.now()
>>> now
DateTime(2020, 1, 28, 19, 15, 24, 146775, tzinfo=Timezone('Europe/Berlin'))
>>> print(now)
2020-01-28T19:15:24.146775+01:00
>>>
>>> now.timezone
Timezone('Europe/Berlin')
>>> now.is_utc()
False
>>>
>>> future = now.add(years=1)
>>> future
DateTime(2021, 1, 28, 19, 15, 24, 146775, tzinfo=Timezone('Europe/Berlin'))
>>> future.diff_for_humans()
'in 1 year'
|