Skip to content

Commit

Permalink
get date like php
Browse files Browse the repository at this point in the history
get date like php format, with delta time in day
  • Loading branch information
nawie committed May 12, 2016
1 parent a9e5205 commit 6fc8fe9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# get date like php format
#
# example : date('d-M-Y'), minus 1 day date('d-M-Y', -1), plus 1 day date('d-M-Y', 1)
# result : 12-05-2016, 11-05-2016, 13-05-2016,
#
def date(format = "", delta = 0):
assert isinstance(format, basestring), "Date format must be a string"
assert isinstance(delta, int), "Date delta must be an integer"
if not format :
format = 'Y-m-d H:M:S'
# check date part
def part(x):
return '%'+x if re.match("[^\W\d_]",x) else x
# check is delta
now = datetime.datetime.now() + datetime.timedelta(days=delta)
# result
return now.strftime(
''.join(map(part, format))
)

0 comments on commit 6fc8fe9

Please sign in to comment.