Python နေ့စွဲအချိန်
Python နေ့စွဲများ
Python ရှိ ရက်စွဲသည် ၎င်း၏ကိုယ်ပိုင်ဒေတာအမျိုးအစားမဟုတ်သော်လည်း datetime
ရက်စွဲအရာဝတ္ထုများအဖြစ် ရက်စွဲများနှင့် အလုပ်လုပ်ရန် အမည်ရှိ မော်ဂျူးတစ်ခုကို တင်သွင်းနိုင်သည်။
ဥပမာ
datetime module ကို တင်သွင်းပြီး လက်ရှိရက်စွဲကို ပြသသည်-
import datetime
x = datetime.datetime.now()
print(x)
ရက်စွဲထုတ်ပေးခြင်း။
အထက်ဖော်ပြပါ ဥပမာမှ ကုဒ်ကို ကျွန်ုပ်တို့ လုပ်ဆောင်သောအခါ ရလဒ်သည်-
2022-01-29 15:58:22.091968
ရက်စွဲတွင် နှစ်၊ လ၊ နေ့၊ နာရီ၊ မိနစ်၊ ဒုတိယနှင့် မိုက်ခရိုစက္ကန့်များ ပါရှိသည်။
မော် datetime
ဂျူးတွင် ရက်စွဲအရာဝတ္တုနှင့်ပတ်သက်သည့် အချက်အလက်ကို ပြန်ပေးရန် နည်းလမ်းများစွာရှိသည်။
ဤသည်မှာ နမူနာအချို့ဖြစ်ပြီး၊ ဤအခန်းတွင် ၎င်းတို့အကြောင်း နောက်မှ လေ့လာနိုင်ပါမည်-
ဥပမာ
နှစ်နှင့် ရက်သတ္တပတ်၏ အမည်ကို ပြန်ပေးပါ။
import datetime
x = datetime.datetime.now()
print(x.year)
print(x.strftime("%A"))
ရက်စွဲအရာဝတ္ထုများဖန်တီးခြင်း။
ရက်စွဲတစ်ခုဖန်တီးရန်၊ ကျွန်ုပ်တို့သည် module datetime()
၏ class (constructor) ကို
သုံးနိုင်သည်။datetime
အတန်းသည် ရက်စွဲတစ်ခုဖန်တီးရန် က datetime()
န့်သတ်ချက်သုံးခု လိုအပ်သည်- တစ်နှစ်၊ လ၊ နေ့။
ဥပမာ
ရက်စွဲအရာဝတ္ထုတစ်ခုဖန်တီးပါ-
import datetime
x = datetime.datetime(2020, 5, 17)
print(x)
အတန်း သည် datetime()
အချိန်နှင့် အချိန်ဇုန် (နာရီ၊ မိနစ်၊ ဒုတိယ၊ မိုက်ခရိုစက္ကန့်၊ tzone) အတွက် ပါရာမီတာများကိုလည်း ယူသည်၊ သို့သော် ၎င်းတို့သည် ချန်လှပ်ထားပြီး 0
၊ ( None
အချိန်ဇုန်အတွက်) ပုံသေတန်ဖိုးတစ်ခုရှိသည်။
strftime() နည်းလမ်း
အရာဝတ္ထု တွင် datetime
ရက်စွဲအရာဝတ္ထုများကို ဖတ်နိုင်သော စာကြောင်းများအဖြစ် ဖော်မတ်ချရန် နည်းလမ်းတစ်ခု ရှိသည်။
နည်းလမ်းကို ၊ ဟုခေါ်ပြီး ပြန်ပေးသောစာကြောင်း၏ဖော်မတ်ကိုသတ်မှတ်ရန် ကန့်သတ် strftime()
ချက်တစ်ခုယူသည်
-format
ဥပမာ
လအမည်ကို ပြပါ-
import datetime
x = datetime.datetime(2018, 6, 1)
print(x.strftime("%B"))
တရားဝင်ဖော်မတ်ကုဒ်များအားလုံး၏ ကိုးကားချက်-
Directive | Description | Example | Try it |
---|---|---|---|
%a | Weekday, short version | Wed | |
%A | Weekday, full version | Wednesday | |
%w | Weekday as a number 0-6, 0 is Sunday | 3 | |
%d | Day of month 01-31 | 31 | |
%b | Month name, short version | Dec | |
%B | Month name, full version | December | |
%m | Month as a number 01-12 | 12 | |
%y | Year, short version, without century | 18 | |
%Y | Year, full version | 2018 | |
%H | Hour 00-23 | 17 | |
%I | Hour 00-12 | 05 | |
%p | AM/PM | PM | |
%M | Minute 00-59 | 41 | |
%S | Second 00-59 | 08 | |
%f | Microsecond 000000-999999 | 548513 | |
%z | UTC offset | +0100 | |
%Z | Timezone | CST | |
%j | Day number of year 001-366 | 365 | |
%U | Week number of year, Sunday as the first day of week, 00-53 | 52 | |
%W | Week number of year, Monday as the first day of week, 00-53 | 52 | |
%c | Local version of date and time | Mon Dec 31 17:41:00 2018 | |
%C | Century | 20 | |
%x | Local version of date | 12/31/18 | |
%X | Local version of time | 17:41:00 | |
%% | A % character | % | |
%G | ISO 8601 year | 2018 | |
%u | ISO 8601 weekday (1-7) | 1 | |
%V | ISO 8601 weeknumber (01-53) | 01 |