-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get date like php format, with delta time in day
- Loading branch information
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
) |