Package: beautiful-date
Beautiful DateSimple and beautiful way to create date and datetime objects in Python.Before:fromdatetimeimportdate,datetimed=date(year=2018,month=3,day=25)t=datetime(year=2018,month=3,day=25,hour=23,minute=45)After:frombeautiful_dateimport*d=25/Mar/2018t=(25/Mar/2018)[23:45]Installationpipinstallbeautiful-dateExamplesCreate DateUsing months names:>>>frombeautiful_dateimport*>>>25/Mar/2018# European formatBeautifulDate(2018,3,25)>>>Mar/25/2018# US formatBeautifulDate(2018,3,25)Using months numbers:>>>25/M[3]/2018# European formatBeautifulDate(2018,3,25)>>>M[3]/25/2018# US formatBeautifulDate(2018,3,25)Or alternatively:>>>D@25/3/2018# European format (default)BeautifulDate(2018,3,25)>>>D=MDY()# Add this at the top of your script to use US format.>>>d=D@3/25/2018# US formatBeautifulDate(2018,3,25)You can also easily retrieve current date as aBeautifulDateobject and current time using:>>>D.today()BeautifulDate(2020,8,24)>>>D.now()datetime.datetime(2020,8,24,0,59,12,451363)>>>D.tomorrow()BeautifulDate(2020,8,25)>>>D.yesterday()BeautifulDate(2020,8,23)Create DatetimePrevious methods createBeautifulDateobjects which are inherited fromdatebut can be
easily extended todatetimeusing indexing/slicing:>>>(Oct/16/1995)[:]datetime.datetime(1995,10,16,0,0)>>>(Oct/16/1995)[23]datetime.datetime(1995,10,16,23,0)>>>(Oct/16/1995)[23:14]datetime.datetime(1995,10,16,23,14)>>>(Oct/16/1995)[23:14:10]datetime.datetime(1995,10,16,23,14,10)You can also use prefixD @if you need months by their numbers:>>>(D@16/10/1995)[:]datetime.datetime(1995,10,16,0,0)>>>(D@16/10/1995)[23]datetime.datetime(1995,10,16,23,0)>>>(D@16/10/1995)[23:14]datetime.datetime(1995,10,16,23,14)>>>(D@16/10/1995)[23:14:10]datetime.datetime(1995,10,16,23,14,10)Date/Datetime manipulations:This library also provides simple interface forrelativedeltafromdateutilNotice that singular time unit (year, month, ...) sets given value, plural (years, months,) adds it.Shortcuts:>>>5*days.from_todayBeautifulDate(2023,9,17)>>>1*hours.from_nowdatetime.datetime(2023,9,12,12,53,56)>>>3*days.since(15/Mar/2023)BeautifulDate(2023,3,18)>>>5*days.until_todayBeautifulDate(2023,9,7)>>>1*hours.until_nowdatetime.datetime(2023,9,12,11,13,4)>>>3*days.until(15/Mar/2023)BeautifulDate(2023,3,12)Adding/Subtracting/Setting timedeltas:>>>d=26/Mar/2018>>>t=d[12:23:15]>>>d+2*yearsBeautifulDate(2020,3,26)>>>d-2*daysBeautifulDate(2018,3,24)>>>t+25*hoursdatetime.datetime(2018,3,27,13,23,15)Available deltas:years,months,weeks,days,hours,minutes,seconds,microseconds,leapdays(seerelativedelta).>>>d=26/Mar/2018>>>t=d[12:23:15]>>>d+2022*yearBeautifulDate(2022,3,26)>>>d+=2*day>>>dBeautifulDate(2018,3,2)>>>t+22*hourdatetime.datetime(2018,3,26,22,23,15)>>>t+=22*hour>>>tdatetime.datetime(2018,3,26,22,23,15)Available setters:year,month,day,hour,minute,second,microsecond,yeardayandnlyearday(seerelativedelta).Weekdays:Get next Monday:>>>d=29/Mar/2018# Thursday>>>d+MO# Equivalent to MO(1)BeautifulDate(2018,4,2)Get second to next Monday:>>>d=29/Mar/2018>>>d+MO(2)BeautifulDate(2018,4,9)Get last Saturday:>>>d=29/Mar/2018>>>d-SABeautifulDate(2018,3,24)Get second to last Saturday:>>>d=29/Mar/2018>>>d-SA(2)BeautifulDate(2018,3,17)Get second to last Saturday (same as previous):>>>d=29/Mar/2018>>>d+SA(-2)BeautifulDate(2018,3,17)Utildrange:You can usedrangeto generate ranges of dates:>>>fordindrange(27/Mar/1994,5/Apr/1994):...print(d)1994-03-271994-03-281994-03-291994-03-301994-03-311994-04-011994-04-021994-04-031994-04-04>>>fordindrange(27/Mar/1994,5/Apr/1994,2*days):...print(d)1994-03-271994-03-291994-03-311994-04-021994-04-04and datetimes:>>>fordtindrange((27/Mar/1994)[10:25],(4/Apr/1994)[10:10]):...print(dt)1994-03-2710:25:001994-03-2810:25:001994-03-2910:25:001994-03-3010:25:001994-03-3110:25:001994-04-0110:25:001994-04-0210:25:001994-04-0310:25:00>>>fordtindrange((27/Mar/1994)[10:25],(4/Apr/1994)[10:10],20*hours):...print(dt)1994-03-2710:25:001994-03-2806:25:001994-03-2902:25:001994-03-2922:25:001994-03-3018:25:001994-03-3114:25:001994-04-0110:25:001994-04-0206:25:001994-04-0302:25:001994-04-0322:25:00