Cara menggunakan python calendar formatmonth

Setiap bahasa pemrograman memiliki cara untuk menangani date and time (tanggal dan waktu) termasuk juga Python. Dalam ilmu komputer dan pemrograman komputer, sistem datetime adalah untuk mewakili gagasan sistem komputer tentang berlalunya waktu. Dalam pengertian ini, “time” juga mencakup berlalunya hari pada kalender.

Program Python date time dapat menangani tanggal dan waktu dalam beberapa cara. Mengonversi antara format tanggal adalah tugas umum untuk komputer. Modul datetime Python membantu melacak tanggal dan waktu.

Table of Contents

Tick, untuk Menangani Time dan Date pada Python

Interval waktu adalah angka floating-point dalam satuan detik. Instan tertentu dalam waktu dinyatakan dalam detik sejak pukul 00:00:00 1 Januari 1970 (epoch). Modul datetime populer yang tersedia di dalam Bahasa pemrograman Python berguna untuk menyediakan fungsi dalam bekerja dengan datetime, serta untuk mengubah antar representasi. Fungsi time.time() mengembalikan waktu sistem saat ini dalam tick sejak pukul 00:00:00 1 Januari 1970(Epoch).

Contoh kode:

#!/usr/bin/python import time; # This is required to include time module. ticks = time.time() print "Number of ticks since 12:00am, January 1, 1970:", ticks

Kode itu akan membuat hasil sebagai berikut :

Number of ticks since 12:00am, January 1, 1970: 7186862.73399

Dengan tick ini, aritmatika tanggal atau waktu dapat lebih mudah dilakukan. Namun, tanggal epoch tidak dapat direpresentasikan dalam bentuk ini. Tanggal di masa depan yang jauh juga tidak dapat direpresentasikan dengan cara ini – titik batasnya sekitar tahun 2038 untuk UNIX dan Windows.

Apa itu TimeTuple?

Banyak fungsi Python date time yang digunakan untuk menangani waktu sebagai tupel dari 9 angka. Hal tersebut seperti yang ditunjukkan di bawah ini:

Dengan tick ini, aritmatika tanggal atau waktu dapat lebih mudah dilakukan. Namun, tanggal epoch tidak dapat direpresentasikan dalam bentuk ini. Tanggal di masa depan yang jauh juga tidak dapat direpresentasikan dengan cara ini – titik batasnya sekitar tahun 2038 untuk UNIX dan Windows.

IndexBidangNilai04-digit tahun20211Bulan1 ke 122Hari1 ke 313Jam0 ke 234Menit0 ke 595Detik0 ke 61 (60 atau 61 adalah detik kabisat)6Hari dalam Minggu0 ke 6 (0 adalah Senin)7Hari dalam tahun1 ke 366 (Tahun kabisat)8Penghematan siang hari-1, 0, 1, -1 mempunyai arti bahwa library menentukan DST

 

Tuple di atas setara dengan struktur struct_time. Struktur Python Date Time ini memiliki atribut seperti berikut:

IndexAtributNilai0tm_year20211tm_mon1 ke 122tm_mday1 ke 313tm_hour0 ke 234tm_min0 ke 595tm_sec0 ke 61 (60 atau 61 adalah detik kabisat)6tm_wday0 ke 6 (0 adalah Senin)7tm_yday1 ke 366 (Tahun kabisat)8tm_isdst-1, 0, 1, -1 yang berarti library menentukan DST

 

Artikel Terkait  Tutorial Python 12 : Dictionary pada Python | Penjelasan Lengkapnya

Mendapatkan waktu saat ini

Untuk menerjemahkan waktu instan dari detik sejak floating-point value epoch menjadi tupel waktu. Anda bisa teruskan floating-point value ke suatu fungsi (misalnya, waktu lokal) yang mengembalikan tupel waktu dengan kesembilan item yang valid.

#!/usr/bin/python import time; localtime = time.localtime(time.time()) print "Local current time :", localtime

Ini akan menghasilkan hasil berikut, yang dapat diformat dalam bentuk lain yang dapat dilihat:

Local current time : time.struct_time(tm_year=2013, tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26, tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

Mendapatkan waktu yang diformat

Anda dapat memformat kapan saja sesuai kebutuhan Anda, tetapi metode sederhana untuk mendapatkan waktu dalam format rule dapat dibaca adalah asctime():

#!/usr/bin/python import time; localtime = time.asctime( time.localtime(time.time()) ) print "Local current time :", localtime

Kode ini akan memberikan hasil sebagai berikut:

Local current time : Tue Des 21 10:17:09 2021

Mendapatkan kalender selama sebulan

Modul kalender dalam Python memberikan berbagai metode untuk melakukan sesuatu terhadap penanggalan tahunan dan bulanan. Di sini, kami mencetak kalender untuk bulan tertentu ( Jan 2022 )

Calendar module in Python has the calendar class that allows the calculations for various task based on date, month, and year. On top of it, the TextCalendar and HTMLCalendar class in Python allows you to edit the calendar and use as per your requirement.

Let see what we can do with Python Calendar.

Step1) Run the code.

  • Code Line # 1: We begin with “import calendar” which will import all the classes of this module.
  • Code Line # 3: c= calendar.TextCalendar(calendar.SUNDAY) tells the interpreter to create a text calendar. Start of the month will be Sunday. In Python, you can format the calendar as you can change the day of the month to begin with
  • Code Line # 4: str= c.formatmonth(2025,1) We are creating calendar for the year 2025, Month 1 – January
  • Code Line # 5: print str will print the output.

Let’s quickly change the value from Sunday to Thursday and check the output

Step 2) You can also print out the Calendar in HTML format, this feature is helpful for developer if they want to make any changes in the look and feel of calendar

Step 3) Loops over the days of a month by using c.itermonthday (2025,4), it will fetch the total number of days for that month.

  • When you execute the code to get the total number of days for a specific month say “April” then you will get 30 days in the output but you will also see some zeros along with these days at the beginning and sometimes at the end of it.
  • Zeros in the output mean that the day of the week is in an overlapping month, which means it does not belong to that month.
  • These zeros appears in output because, in your code you have mentioned day (Thursday), so when you call function “c.itermonthdays”, it will start counting days from Thursday and your Thursday may not start with date 1st of April it might be 28th or 29th of March, so when you execute the code it will start counting days from 28th of march and any days after that till 1st of April. These days will be counted as zero and in the output you will see these zeroes and same is applicable to the end of the month.
  • So except date 1-30 all the dates from previous as well as post month will appear in the output as zeroes.

Step 4) You can fetch the data from the local system, like months or weekdays, etc

  • The output over here shows that we have printed out the months name from the local system. Likewise, you can also fetch the weekdays name as shown below
  • The output will depend on the local system, suppose if your local system is some other countries then it will give the output as per the local settings of that country. Here we have months so it won’t be a difference but if it is a week or day, it will certainly differ.

Step 5) You can fetch the list of the specific day for a whole year. For example, there is an audit day on every first Monday of a week. You want to know the date of first Monday for each month. You can use this code

Postingan terbaru

LIHAT SEMUA