কম্পিউটার টিউটোরিয়াল

পাইথনে ডেট ভ্যালিডেশন কীভাবে করবেন?


পাইথনে আপনি যে ডেট ভ্যালিডেশন অর্জন করতে চান সেটি বড় মাত্রায় আপনার থাকা ডেট ফরম্যাটের ওপর নির্ভর করবে। datetime লাইব্রেরির strptime ফাংশন স্ট্রিংকে ডেট/টাইমে পার্স করতে ব্যবহার করা যায়।

উদাহরণ

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 the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.
%w
Weekday as a decimal number [0(Sunday),6].
%W
Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.
%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



  1. পাইথন প্রোগ্রাম দিয়ে দশমিক সংখ্যাকে বাইনারি নম্বরে রূপান্তর

  2. পাইথনে ফিবোনাচ্চি সংখ্যা কিনা তা যাচাই করার প্রোগ্রাম

  3. পাইথনে পান্ডাস অফসেটকে পাইথন ডেটে কনভার্ট করবেন?

  4. পাইথনে ফরম্যাট করা ডেট ও টাইম কীভাবে পাবেন?