Tutorial Python

RUMAH Python Pengenalan Python Python Memulai Sintaks Python Komentar Python Variabel Python Tipe Data Python Nomor Python Pengecoran Python String Python Python Boolean Operator Python Daftar Python Tuple Python Set Python Kamus Python Python Jika...Lain Python Sementara Loop Python Untuk Loop Fungsi Python Python Lambda Array Python Kelas/Objek Python Warisan Python Python Iterator Lingkup Python Modul Python Tanggal Python Python Matematika Python JSON Python RegEx Python PIP Python Coba...Kecuali Masukan Pengguna Python Pemformatan String Python

Penanganan Berkas

Penanganan File Python File Baca Python Python Tulis/Buat File Python Hapus File

Modul Python

Tutorial NumPy Panduan Panda Tutorial sip

Python Matplotlib

Pengantar Matplotlib Matplotlib Memulai Matplotlib Pyplot Merencanakan Matplotlib Penanda Matplotlib Garis Matplotlib Label Matplotlib Kotak Matplotlib Subplot Matplotlib Penyebaran Matplotlib Matplotlib Bar Histogram Matplotlib Bagan Pai Matplotlib

Pembelajaran mesin

Mulai Mode Median Rata-rata Standar Deviasi Persentil Distribusi Data Distribusi Data Normal Plot Pencar Regresi linier Regresi Polinomial Regresi Berganda Skala Kereta/Tes Pohon Keputusan

Python MySQL

MySQL Memulai MySQL Buat Basis Data MySQL Buat Tabel Sisipan MySQL MySQL Pilih MySQL Dimana MySQL Dipesan Oleh Hapus MySQL Tabel Drop MySQL Pembaruan MySQL Batas MySQL MySQL Bergabung

Python MongoDB

MongoDB Memulai MongoDB Buat Basis Data MongoDB Buat Koleksi Sisipan MongoDB Temukan MongoDB Permintaan MongoDB Sortir MongoDB Hapus MongoDB Koleksi Jatuhkan MongoDB Pembaruan MongoDB Batas MongoDB

Referensi Python

Ikhtisar Python Fungsi bawaan Python Metode String Python Metode Daftar Python Metode Kamus Python Metode Tuple Python Metode Set Python Metode File Python Kata Kunci Python Pengecualian Python Daftar Istilah Python

Referensi Modul

Modul Acak Modul Permintaan Modul Statistik Modul Matematika Modul cMath

Python Bagaimana caranya?

Hapus Duplikat Daftar Membalikkan String Tambahkan Dua Angka

Contoh Python

Contoh Python Kompilator Python Latihan Python Kuis Python Sertifikat Python

Waktu Tanggal Python


Tanggal Python

Tanggal dalam Python bukanlah tipe datanya sendiri, tetapi kita dapat mengimpor modul bernama datetimeuntuk bekerja dengan tanggal sebagai objek tanggal.

Contoh

Impor modul datetime dan tampilkan tanggal saat ini:

import datetime

x = datetime.datetime.now()
print(x)

Keluaran Tanggal

Ketika kita mengeksekusi kode dari contoh di atas, hasilnya adalah:

2022-01-29 15:49:26.751512

Tanggal berisi tahun, bulan, hari, jam, menit, detik, dan mikrodetik.

Modul datetimeini memiliki banyak metode untuk mengembalikan informasi tentang objek tanggal.

Berikut adalah beberapa contoh, Anda akan mempelajari lebih lanjut tentang mereka nanti di bab ini:

Contoh

Kembalikan tahun dan nama hari kerja:

import datetime

x = datetime.datetime.now()

print(x.year)
print(x.strftime("%A"))

Membuat Objek Tanggal

Untuk membuat tanggal, kita dapat menggunakan datetime()kelas (konstruktor) dari datetimemodul.

Kelas datetime()membutuhkan tiga parameter untuk membuat tanggal: tahun, bulan, hari.

Contoh

Buat objek tanggal:

import datetime

x = datetime.datetime(2020, 5, 17)

print(x)

Kelas datetime()juga mengambil parameter untuk waktu dan zona waktu (jam, menit, detik, mikrodetik, tzone), tetapi mereka opsional, dan memiliki nilai default 0, ( Noneuntuk zona waktu).



Metode strftime()

Objek datetimememiliki metode untuk memformat objek tanggal menjadi string yang dapat dibaca.

Metode ini dipanggil strftime(), dan mengambil satu parameter, format, untuk menentukan format string yang dikembalikan:

Contoh

Menampilkan nama bulan:

import datetime

x = datetime.datetime(2018, 6, 1)

print(x.strftime("%B"))

Referensi semua kode format hukum:

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