การแปลงวันที่เป็นสตริง

การทำงานกับวันที่และเวลาใน Python

Max Shron

Data Scientist and Author

รูปแบบ ISO 8601

from datetime import date

# Example date
d = date(2017, 11, 5)

# ISO format: YYYY-MM-DD print(d)
2017-11-05
# Express the date in ISO 8601 format and put it in a list
print( [d.isoformat()] )
['2017-11-05']
การทำงานกับวันที่และเวลาใน Python

รูปแบบ ISO 8601

# A few dates that computers once had trouble with
some_dates = ['2000-01-01', '1999-12-31']

# Print them in order print(sorted(some_dates))
['1999-12-31', '2000-01-01']
การทำงานกับวันที่และเวลาใน Python

รูปแบบอื่น ๆ

d.strftime()
การทำงานกับวันที่และเวลาใน Python

รูปแบบอื่น ๆ: strftime

# Example date
d = date(2017, 1, 5)

print(d.strftime("%Y"))
2017
# Format string with more text in it
print(d.strftime("Year is %Y"))
Year is 2017
การทำงานกับวันที่และเวลาใน Python

รูปแบบอื่น ๆ: strftime

# Format: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))
2017/01/05
การทำงานกับวันที่และเวลาใน Python

การแปลงวันที่เป็นสตริง

การทำงานกับวันที่และเวลาใน Python

Preparing Video For Download...