天天看點

python擷取目前時間的前一天,前一周,前一個月

import

datetime

d

=

datetime.datetime.now()

def

day_get(d):

oneday

=

datetime.timedelta(days

=

1

)

day

=

d

-

oneday

date_from

=

datetime.datetime(day.year, day.month, day.day,

,

,

)

date_to

=

datetime.datetime(day.year, day.month, day.day,

23

,

59

,

59

)

print

'---'

.join([

str

(date_from),

str

(date_to)])

def

week_get(d):

dayscount

=

datetime.timedelta(days

=

d.isoweekday())

dayto

=

d

-

dayscount

sixdays

=

datetime.timedelta(days

=

6

)

dayfrom

=

dayto

-

sixdays

date_from

=

datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day,

,

,

)

date_to

=

datetime.datetime(dayto.year, dayto.month, dayto.day,

23

,

59

,

59

)

print

'---'

.join([

str

(date_from),

str

(date_to)])

def

month_get(d):

dayscount

=

datetime.timedelta(days

=

d.day)

dayto

=

d

-

dayscount

date_from

=

datetime.datetime(dayto.year, dayto.month,

1

,

,

,

)

date_to

=

datetime.datetime(dayto.year, dayto.month, dayto.day,

23

,

59

,

59

)

print

'---'

.join([

str

(date_from),

str

(date_to)])

轉載自http://www.oschina.net/code/snippet_736230_26816