Manufaktur industri
Industri Internet of Things | bahan industri | Pemeliharaan dan Perbaikan Peralatan | Pemrograman industri |
home  MfgRobots >> Manufaktur industri >  >> Industrial programming >> Python

Tutorial KALENDER Python dengan Contoh

Modul kalender di Python memiliki kelas kalender yang memungkinkan perhitungan untuk berbagai tugas berdasarkan tanggal, bulan, dan tahun. Selain itu, kelas TextCalendar dan HTMLCalendar dengan Python memungkinkan Anda untuk mengedit kalender dan menggunakannya sesuai kebutuhan Anda.

Mari kita lihat apa yang bisa kita lakukan dengan Kalender Python.

Langkah1) Jalankan kodenya.

Mari kita ubah nilainya dengan cepat dari Minggu ke Kamis dan periksa hasilnya

Langkah 2) Anda juga dapat mencetak Kalender dalam format HTML, fitur ini berguna bagi pengembang jika mereka ingin melakukan perubahan pada tampilan dan nuansa kalender

Langkah 3) Loop selama hari dalam sebulan dengan menggunakan c.itermonthday (2025,4), itu akan mengambil jumlah total hari untuk bulan itu.

Langkah 4) Anda dapat mengambil data dari sistem lokal, seperti bulan atau hari kerja, dll

Langkah 5) Anda dapat mengambil daftar hari tertentu selama setahun penuh. Misalnya, ada hari audit pada setiap Senin pertama dalam seminggu. Anda ingin mengetahui tanggal Senin pertama setiap bulannya. Anda dapat menggunakan kode ini

Ini kode lengkapnya

Contoh Python 2

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print str

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print str
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print i

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print name
    for day in calendar.day_name:
        print day
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print "%10s %2d" % (calendar.month_name[month], auditday)

Contoh Python 3

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print(str)

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print(i)

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print(name)
    for day in calendar.day_name:
        print(day)
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print("%10s %2d" % (calendar.month_name[month], auditday))

Ringkasan:


Python

  1. Tutorial Kelas Abstrak C# dengan Contoh:Apa itu Abstraksi?
  2. Python String strip() Fungsi dengan CONTOH
  3. Python String count() dengan CONTOH
  4. Python round() fungsi dengan CONTOH
  5. Python map() berfungsi dengan CONTOH
  6. Python Timeit() dengan Contoh
  7. Hasil dalam Python Tutorial:Generator &Hasil vs Contoh Pengembalian
  8. Penghitung Python dalam Koleksi dengan Contoh
  9. Daftar Python count() dengan CONTOH
  10. Indeks Daftar Python () dengan Contoh