Introduction to Programming with Python Date and Time සමඟ වැඩකරන හැටි Dileepa Rajapaksa | http://www.windowsgeek.lk @dsrajapaksa Microsoft Virtual Academy
කලින් Video එකේ අපි කතා කලේ මොනවාද? පරිශීලකයාගෙන් සංඛ්යාවක් ආදානය ලෙස ලබා ගන්නා අයුරු Variable තුල සංඛ්යා ගබඩා කිරීම Variable තුල ඇති සංඛ්යා නැවත භාවිත කර ගණිත කර්ම … සුලභ භාවිත වන ගණිත කර්ම ගණිතකර්ම කිරීමේ ප්රමුඛතා අනුපිළිවල. Datatype එකක් තවත් Datatype එකකට හැරවීම Microsoft Virtual Academy
Demo කලින් Video එකේ අභියෝගයට පිළිතුරු Microsoft Virtual Academy
දින සහ වේලාවන් සමග වැඩ කිරීම DateTime Last part Practice your language Microsoft Virtual Academy
අපි ගොඩක් වෙලාව ගතකරනවා deadline හා schedule ගැන හිතමින් මගේ උපන්දිනයට දින කීයද? මගේ Project එක බාරදෙන්න ඕනේ කවදාද? දින දෙකකින් appointment එකක් දාන්න ඕනෙ, ඒ දවස කවදා වේවිද? Microsoft Virtual Academy
මේ වගේ ගැටළු විසඳීමේදී අපිට දින සහ වේලාවන් ගබඩා කිරීම සහ හැසිරවීම ගැන ඉගෙනගන්න වෙනවා. Microsoft Virtual Academy
තමන්ගේ උපන්දිනයට ඇති දින ගණන සොයාගැනීමට අවශ්යනම් මුලින්ම අද දිනය දැනගතයුතුයි. datetime කියන class එක මගින් අපිට අද දිනය ලබාගන්න පුළුවන්. #The import statement gives us access to #the functionality of the datetime class import datetime #today is a function that returns today's date print (datetime.date.today()) Microsoft Virtual Academy
දිනයන් variable වල ගබඩාකරන්න පුළුවන්. import datetime #store the value in a variable called currentDate currentDate = datetime.date.today() print (currentDate) Microsoft Virtual Academy
Demo දැන් වේලාව හා දිනය පෙන්වීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
දිනයෙහි ඇති වෙනස් කොටස් ඔබට භාවිතාකරන්න පුළුවන්. import datetime currentDate = datetime.date.today() print (currentDate) print (currentDate.year) print (currentDate.month) print (currentDate.day) Microsoft Virtual Academy
Demo දිනයෙහි කොටස් භාවිතය සඳහා date functions එක භාවිතාකිරීම Last part Practice your language Microsoft Virtual Academy
Date formats Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
2/5/2014 මගින් පෙන්වන්නේ මොන දවසද? Microsoft Virtual Academy
වෙනත් format එකකට දිනය පෙන්වන්න ඕනෙනම් මොකද කරන්නේ ? විවිධ රටවල විවිධ පරිශීලකයින් විවිධ date format භාවිතාකරනවා, default ක්රමය ඔබට අවශ්ය විදිහ නොවෙන්න පුළුවන්. මේ දේවල් වලටත් විසඳුම් තියෙනවා, ඒත් ටිකක් වෙලාව යනවා ඒ වගේම වැඩිපුර code ලියන්න වෙනවා. default format එක YYYY-MM-DD Microsoft Virtual Academy
Python වලදී strftime භාවිතාකරලා date format එක වෙනස්කරන්න පුළුවන්. import datetime currentDate = datetime.date.today() #strftime allows you to specify the date format print (currentDate.strftime('%d %b,%Y')) Microsoft Virtual Academy
මොනවද මේ %d %b සහ %Y? %d කියන්නේ දිනය %b මාසය හා කෙටි යෙදුම (eg : Jan , Feb ) %Y අංක 4 කින් යුත් අවුරුද්ද Microsoft Virtual Academy
තවත් අවශ්යවෙන්න පුළුවන් දේවල් %b මාසයේ කෙටිනම %B මාසයේ සම්පූර්ණ නම %y අංක 2කින් අවුරුද්ද දැක්වීමට %a දවස කෙටියෙන් දැක්වීමට %A දවස සම්පූර්ණයෙන් දැක්වීමට Strftime සම්පූර්ණ ලැයිස්තුව සඳහා strftime.org යන්න. Microsoft Virtual Academy
Demo Formatting dates Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
Wedding Invitation එකක් print කරන්න පුළුවන්ද? “Please attend our event Sunday, July 20 in the year 1997” import datetime currentDate = datetime.date.today() #strftime allows you to specify the date format print (currentDate.strftime ('Please attend our event %A, %B %d in the year %Y')) Microsoft Virtual Academy
ඉංග්රීසි වෙනුවට සිංහල වගේ වෙනත් භාෂාවක්…. Localization කියලයි ඒකට කියන්නෙ සාමාන්යයෙන් Program එක භාවිතාකරන්නේ පරිගණකය භාවිතාකරණ භාෂාව. ඒත් අපිට පරිගණකයේ Settings වලින් හැම විටම බලාපොරොත්තු වෙන්න බැහැ. අපිට පුළුවන් Python වලට කියන්න එක් භාෂාවක් පමණක් භාවිතාකරන්න කියලා. ඒකට තවත් code සහ වෙලාව වැයවෙනවා.ඒවා ගැන දැන්ම කතාකරන්නේ නැහැ.හොයලාබලන්න ඕනෙනම් මෙතනට යන්න babel Python library http://babel.pocoo.org/ Microsoft Virtual Academy
උපන්දිනයට ඇති දින ගණන ගනිමු,ඒ සඳහා මට User ගෙන් birthday එක අහන්න වෙනවා. birthday = input ("What is your birthday? ") print ("Your birthday is " + birthday) birthday එකේ datatype එක මොකක්ද? string අපට එය දිනයක් වශයෙන් සැලකියයුතුනම් ( උදා: අපි datetime function එක භාවිතා කරලා format එක වෙනස් කරලා print කලා වගේ ) අප විසින් එය දිනයක් බවට පරිවර්ථනය කරන්න වෙනවා. Microsoft Virtual Academy
strptime function එක මගින් ඔබට පුළුවන් string එකක් දිනයක් බවට පරිවර්තනය කරන්න import datetime birthday = input ("What is your birthday? ") birthdate = datetime.datetime.strptime(birthday,"%m/%d/%Y").date() #why did we list datetime twice? #because we are calling the strptime function #which is part of the datetime class #which is in the datetime module print ("Your birth month is " + birthdate.strftime('%B')) Microsoft Virtual Academy
Demo User ගෙන් date value එකක් ලබාගැනීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
User විසින් අපි strptime function එකේ දක්වලා තියෙන විදිහට වඩා වෙනස් විදිහකට දිනය ඇතුලත්කලොත්… birthdate = datetime.datetime.strptime(birthday,"%m/%d/%Y") Program එක crash වෙනවා අපට අවශ්ය date format එක userට කියන්න සිදුවෙනවා. birthday = input ("What is your birthday? (mm/dd/yyyy)") මෙවැනි ගැටලු හසුරවන අයුරු අප පසුවට සාකච්ඡාකරනවා Microsoft Virtual Academy
Dates ටිකක් අවුල් වගේද. , ඒක වටිනවද Dates ටිකක් අවුල් වගේද? , ඒක වටිනවද? ඇයි නැත්තෙ ඒවා string විදිහට store කරන්න. ඔයාලට ලොකු උත්සවයකට හෝ නිවාඩුවකට ඇති දින ගණන පෙන්වීමට countdown එකක් හදන්න පුළුවන් nextBirthday = \ datetime.datetime.strptime(‘02/04/2016','%m/%d/%Y').date() currentDate = datetime.date.today() #If you subtract two dates you get back the number of days #between those dates difference = nextBirthday - currentDate print (difference.days) Microsoft Virtual Academy
Dates ටිකක් අවුල් වගේද. , ඒක වටිනවද Dates ටිකක් අවුල් වගේද? , ඒක වටිනවද? ඇයි නැත්තෙ ඒවා string විදිහට store කරන්න. ශීතකරණයේ තියෙන ද්රව්යවල කල් ඉකුත්වන දිනයපෙන්වීම currentDate = datetime.date.today() #timedelta allows you to specify the time #to add or subtract from a date print (currentDate + datetime.timedelta(days=15)) print (currentDate + datetime.timedelta(hours=15)) Microsoft Virtual Academy
ඔබට Dates සමඟ තවත් වැඩ සිදු කරන්න වෙනවද? ඔබට අවශ්යදේ datetime වල නැත්නම් dateutil library එක බලන්න (උදා: දින දෙකක් අතර ඇති පරතරය අවුරුදු වලින් ලබාගැනීමට අවශ්යනම් ) Dateutil library : http://labix.org/python-dateutil Microsoft Virtual Academy
වේලාව සමග වැඩකිරීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
වෙලාව ගැන මොකද? Datetime කියන එකේ අපිට වෙලාවත් store කරන්න පුළුවන්. import datetime currentTime = datetime.datetime.now() print (currentTime) print (currentTime.hour) print (currentTime.minute) print (currentTime.second) Microsoft Virtual Academy
dates වගේම time format කරන්නත් strftime() භාවිතාකරන්න පුළුවන්. import datetime currentTime = datetime.datetime.now() print (datetime.datetime.strftime(currentTime,'%H:%M')) %H පැය ( පැය 24 වෙලාවෙන් ) %I පැය ( පැය 12 වෙලාවෙන් ) %p AM හෝ PM %m මිනිත්තු %S තත්පර Microsoft Virtual Academy
Demo වේලාවන් සමග වැඩකිරීම Microsoft Virtual Academy Last part Practice your language Microsoft Virtual Academy
ඔබේ අභියොගය Project එකක අවසාන දිනය user ගෙන් ලබාගන්න. පිළිතුර සති, දින හා පැය වල එකතුවක් ලෙස ලබාදෙන්න. ඉඟිය: ඔබට numeric value video එකේ සාකච්ඡාකල math functions අවශ්ය වේවි. Microsoft Virtual Academy
සුභ පැතුම් ! දැන් ඔබට Date Time සමඟ වැඩකරන Program එකක් ලියන්න හැකියාව තියෙනවා Microsoft Virtual Academy
සාරාංශය… datetime class එක import කරන හැටි Dates format කරන හැටි Strftime function එක භාවිතා කරන අයුරු Strptime function එක භාවිතා කරන අයුරු Times සමඟ වැඩකරන අයුරු Microsoft Virtual Academy