আপনি প্রধানত জাভাস্ক্রিপ্ট ডেটকে পাইথন ডেট অবজেক্টে রূপান্তর করতে চান যখন নেটওয়ার্ক কানেকশন (AJAX রিকোয়েস্ট বা IPC মেসেজ nodejs-এ) এর মাধ্যমে পাঠান। এই রিপ্রেজেন্টেশন স্ট্রিং ফরম্যাটে হবে এবং আপনি যেকোনো ফরম্যাটে পাঠাতে পারেন। স্ট্রিংকে পাইথন ডেট অবজেক্টে প্যার্স করতে strptime ফাংশন ব্যবহার করতে পারেন। ISO 8601 এর মত স্ট্যান্ডার্ডাইজড উপায় আছে ডেট ও টাইম অবজেক্ট পাঠানোর জন্য। একটি সহজ উদাহরণ বিবেচনা করলে বোঝা সহজ হবে।
উদাহরণ
import datetime # জাভাস্ক্রিপ্ট থেকে পাওয়া স্ট্রিং date_string = '2017-12-31' date_format = '%Y-%m-%d' try: date_obj = datetime.datetime.strptime(date_string, date_format) print(date_obj) except ValueError: print("Incorrect data format, should be YYYY-MM-DD")
আউটপুট
এটি নিচের আউটপুট দেবে:
2017-12-31 00:00:00
আপনি অন্যান্য ডিরেক্টিভও ব্যবহার করতে পারেন ডেট প্যার্স করতে। strptime()'-এর ফরম্যাট স্ট্রিংয়ের সমর্থিত ডিরেক্টিভগুলো নিচে:
| Directive | Meaning |
| %a | Locale's abbreviated weekday name. |
| %A | Locale's full weekday name. |
| %b | Locale's abbreviated month name. |
| %B | Locale's full month name. |
| %c | Locale's appropriate date and time representation. |
| %d | Day of the month as a decimal number [01,31]. |
| %H | Hour (24-hour clock) as a decimal number [00,23]. |
| %I | Hour (12-hour clock) as a decimal number [01,12]. |
| %j | Day of the year as a decimal number [001,366]. |
| %m | Month as a decimal number [01,12]. |
| %M | Minute as a decimal number [00,59]. |
| %p | Locale's equivalent of either AM or PM. |
| %S | Second as a decimal number [00,61]. |
| %U | Week number of the year (Sunday as first day) [00,53]. |
| %w | Weekday as a decimal number [0(Sunday),6]. |
| %W | Week number of the year (Monday as first day) [00,53]. |
| %x | Locale's appropriate date representation. |
| %X | Locale's appropriate time representation. |
| %y | Year without century as a decimal number [00,99]. |
| %Y | Year with century as a decimal number. |
| %Z | Time zone name (no characters if no time zone exists). |
| %% | A literal "%" character. |