Python cung cấp phương thức
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
5, trong lớpfrom datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
6 của nó, để chuyển đổi biểu diễn chuỗi của ngày/giờ thành đối tượng ngày.from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
5 method, in itsfrom datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
6 class, to convert a string representation of the date/time into a date object.Cú pháp
Cú pháp cho phương thức
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
5 là:Mã số
Đoạn mã bên dưới minh họa việc sử dụng phương thức
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
5 trong Python:from datetime import datetime
date_time_str = '18/09/19 01:55:19'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
Phương thức
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
5 sẽ không hoạt động nếu đối số chuỗi không phù hợp với tham số định dạng. Điều này được hiển thị dưới đây:from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
Để tạo một ngày, chúng ta có thể sử dụng lớp DateTime () (Trình xây dựng) của mô -đun DateTime. Lớp DateTime () yêu cầu ba tham số để tạo một ngày: năm, tháng, ngày.
Làm cách nào để chuyển đổi một chuỗi thành một ngày trong gấu trúc?
>>> datetime.datetime.strptime( ... "March 5, 2014, 20:13:50", "%B %d, %Y, %H:%M:%S" ... ).replace(tzinfo=datetime.timezone(datetime.timedelta(hours=-3)))Sử dụng phương thức pandas.to_dateTime () được sử dụng để thay đổi thời gian chuỗi/thời gian đối tượng cho đến ngày (DateTime64 [ns]). Phương pháp này đủ thông minh để thay đổi các định dạng khác nhau của cột Ngày chuỗi cho đến nay.
>>> tehran = pytz.timezone("Asia/Tehran") >>> local_time = tehran.localize( ... datetime.datetime.strptime("March 5, 2014, 20:13:50", ... "%B %d, %Y, %H:%M:%S") ... ) >>> local_time datetime.datetime(2014, 3, 5, 20, 13, 50, tzinfo=<DstTzInfo 'Asia/Tehran' +0330+3:30:00 STD>)Làm cách nào để chuyển đổi một chuỗi thành dấu thời gian trong Python?
>>> utc_time = local_time.astimezone(pytz.utc) >>> utc_time datetime.datetime(2014, 3, 5, 16, 43, 50, tzinfo=<UTC>)Nhập thư viện DateTime. Sử dụng DateTime. Lớp DateTime để xử lý các kết hợp ngày và thời gian. Sử dụng phương thức Strptime để chuyển đổi một chuỗi DateTime thành một đối tượng DateTime.
>>> chicago = pytz.timezone("America/Chicago") >>> weird_time = chicago.localize( ... datetime.datetime.strptime("November 18, 1883, 11:00:00", ... "%B %d, %Y, %H:%M:%S") ... ) >>> weird_time.astimezone(pytz.utc) datetime.datetime(1883, 11, 18, 7, 34, tzinfo=<UTC>)Nhiều dấu thời gian có một múi giờ ngụ ý. Để đảm bảo rằng mã của bạn sẽ hoạt động trong mỗi múi giờ, bạn nên sử dụng UTC trong nội bộ và đính kèm múi giờ mỗi khi một đối tượng nước ngoài vào hệ thống.
Phương thức Strptime () có sẵn trong các mô -đun DateTime và Time để phân tích chuỗi vào các đối tượng DateTime và Time.strptime() method is available under datetime and time modules to parse the string to datetime and time objects.
Để chuyển đổi chuỗi thành DateTime trong Python, hãy sử dụng phương thức Strptime (). Strptime () là một phương thức tích hợp của lớp DateTime được sử dụng để chuyển đổi biểu diễn chuỗi của ngày/thời gian sang đối tượng DateTime.string to datetime in Python, use the strptime() method. The strptime() is a built-in method of datetime class used to convert a string representation of the date/time to a datetime object.
Cú pháp
datetime.strptime(date_string, format)Thông số
Hàm strptime () có cả các đối số bắt buộc và nên là một chuỗi. Hàm strptime () hoàn toàn đối diện với hàm strftime (), chuyển đổi một đối tượng DateTime thành một chuỗi.
Thí dụ
# app.py from datetime import datetime dt_str = '27/10/20 05:23:20' dt_obj = datetime.strptime(dt_str, '%d/%m/%y %H:%M:%S') print("The type of the date is now", type(dt_obj)) print("The date is", dt_obj)Đầu ra
The type of the date is now <class 'datetime.datetime'> The date is 2020-10-27 05:23:20DateTime.strptime () là một phương thức chung để phân tích các chuỗi vào các dữ liệu. Nó có thể xử lý tất cả các loại định dạng, với định dạng được xác định bởi chuỗi định dạng bạn đưa ra.datetime.strptime() is a general method for parsing strings into datetimes. It can handle all sorts of formats, with the format defined by the format string you give.
Chuyển đổi chuỗi thành DateTime bằng DateUtil
DateUtil có thể được cài đặt từ PYPI bằng Trình quản lý gói PIP. Nhập gói DateUtil và sử dụng mô -đun phân tích cú pháp để chuyển đổi chuỗi thành DateTime.dateutil can be installed from PyPI using the pip package manager. Import the dateutil package and use the parser module to convert string to datetime.
# app.py from dateutil import parser dt_str = '27/10/20 05:23:20' dt_obj = parser.parse(dt_str) print("The type of the date is now", type(dt_obj)) print("The date is", dt_obj)Đầu ra
The type of the date is now <class 'datetime.datetime'> The date is 2020-10-27 05:23:20Chuỗi python cho đến ngày đối tượngString to date object
Để chuyển đổi chuỗi đến ngày đối tượng trong python, & nbsp; sử dụng hàm date () với hàm strptime ().strptime() function.
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
1Đầu ra
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
2Hàm strptime () sẽ không hoạt động nếu đối số chuỗi không phù hợp với tham số định dạng.
Cách đặt Locale trong Python
Để đặt locale trong Python, hãy nhập gói địa phương trong chương trình của bạn và sau đó sử dụng phương thức Locale.setLocale () để đặt Locale.set a locale in Python, import the locale package in your program and then use the locale.setlocale() method to set the locale.
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
3Đầu ra
from datetime import datetime
date_time_str = '180919 015519'
date_time_obj = datetime.strptime(date_time_str, '%d/%m/%y %H:%M:%S')
print ("The type of the date is now", type(date_time_obj))
print ("The date is", date_time_obj)
4Đó là nó để chuyển đổi một chuỗi thành DateTime trong một bài viết của Python.
Xem thêm
Python DateTime to String
Định dạng ngày Python