Common datetime time processing methods in Python

Source: Internet
Author: User
Tags timedelta

Common datetime time processing methods in Python

Python provides multiple built-in modules for date and time operations, such as calendar, time, and datetime. Today, we will mainly discuss how to use datetime. If you need it, you can refer to it.

 

 

Common time conversion and processing functions:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

Import datetime

# Obtain the current time

D1 = datetime. datetime. now ()

Print d1

# The current time plus half an hour

D2 = d1 + datetime. timedelta (hours = 0.5)

Print d2

# Formatting string output

D3 = d2.strftime ('% Y-% m-% d % H: % M: % s ')

Print d3

# Convert a string to a time type

D4 = datetime. datetime. strptime (date, '% Y-% m-% d % H: % M: % S. % F ')

Print d4

Obtain the date of this week and the first day of this month:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#-*-Coding: UTF-8 -*-

Import datetime

Def first_day_of_month ():

'''

Obtain the first day of this month

: Return:

'''

# Now_date = datetime. datetime. now ()

# Return (now_date + datetime. timedelta (days =-now_date.day + 1). replace (hour = 0, minute = 0, second = 0,

# Microsecond = 0)

Return datetime. date. today ()-datetime. timedelta (days = datetime. datetime. now (). day-1)

Def first_day_of_week ():

'''

Obtain the first day of the week

: Return:

'''

Return datetime. date. today ()-datetime. timedelta (days = datetime. date. today (). weekday ())

If _ name _ = "_ main __":

This_week = first_day_of_week ()

Last_week = this_week-datetime. timedelta (days = 7)

This_month = first_day_of_month ()

Last_month = this_month-datetime. timedelta (days = (this_month-datetime. timedelta (days = 1). day)

Print this_week

Print last_week

Print this_month

Print last_month

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

#! /Usr/bin/python

# Coding = UTF-8

 

Import datetime

 

"""

Powerful functions of datetime

Supported from 0001 to 9999

"""

 

"""

Current Time

Returns a datetime type.

The now method has a parameter tz to set the time zone type. If the effect is not the same as that of the today method

"""

Now = datetime. datetime. now ()

# UTC time

Datetime. datetime. utcnow ()

Attrs = [

("Year", "year"), ('month', "month'), (" day "," day "), ('hour", "hour "), ('minute ', "minute"), ('second', "second"), ('microsecond', "millisecond "),(

'Min', "min"), ('max ', "max "),

]

For k, v in attrs:

"Now. % s = % s # % s" % (k, getattr (now, k), v)

 

 

"""

Returns a time structure.

"""

Now. timetuple ()

 

"""

Returns a date type.

 

"""

Now. date ()

 

"""

Returns a time type.

 

"""

Now. time ()

 

"""

The current day of the week. Monday is 0, and the week is 6

Note that the method is not an attribute.

"""

Now. weekday ()

 

"""

The current day of the week. Monday is 1, week is 7

Note that the method is not an attribute.

"""

Now. isoweekday ()

 

"""

Modify the current time. For example, change it to the 1st day of the current month.

"""

Now. replace (day = 1)

 

Past = datetime. datetime (2010,11, 12,13, 14,15, 16)

 

"""

Comparison

The returned value is of the timedelta type.

 

"""

Now-past

"""

Convert to string

For detailed rules, see Time

 

"""

Strdatetime = now. strftime ("% Y-% m-% d % H: % M: % S ")

"""

String generation datetime object

"""

Datetime. datetime. strptime (strdatetime, "% Y-% m-% d % H: % M: % S ")

The above is all the content of this article. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.