Cara menggunakan time millisecond python

Pada artikel ini akan disajikan teknik manipulasi data tanggal dan waktu menggunakan modul datetime (pada standard library) yang disediakan python. Keberadaan modul ini sangat mempermudah kita saat harus bekerja dengan tanggal dan waktu dalam kode program kita.

Berikut beberapa contoh cara menggunakan modul tersebut untuk berbagai keperluan yang sering kita jumpai dalam pemrograman.

Mendapatkan tanggal saat ini

import datetime

tanggal_saat_ini = datetime.date.today() 
print(tanggal_saat_ini) # tanggal pada hari ini akan ditampilkan di layar

Mendapatkan tanggal dan waktu saat ini

import datetime

saat_ini = datetime.datetime.now() 
print(saat_ini) # waktu saat ini akan ditampilkan di layar

Mengisi variabel dengan tanggal tertentu

from datetime import date

tgl = date(2019, 7, 31) # tahun, bulan, tanggal
print(tgl)

Catatan: tgl pada contoh di atas bertipe date object

Mengakses tahun, bulan, tanggal dari sebuah date object

from datetime import date

hari_ini = date.today() 
print("Tahun ini:", hari_ini.year)
print("Bulan ini:", hari_ini.month)
print("Tanggal hari ini:", hari_ini.day)

Mengisi variabel dengan waktu tertentu

from datetime import time
cth_waktu = time(20, 31, 7) # parameter: jam, menit,detik
print(cth_waktu)

cth_waktu = time(hour = 8, second = 56) # 
print(cth_waktu)

cth_waktu = time(1, 11, 27, 991727) # parameter: jam, menit,detik, microsecond
print(cth_waktu)

catatan: cth_waktu pada contoh di atas bertipe time object

Mengakses jam, menit, detik, dan microsecond dari sebuah time object

from datetime import time

a = time(5, 15, 5, 728172)

print("jam =", a.hour)
print("menit =", a.minute)
print("detik =", a.second)
print("microsecond =", a.microsecond)

Menghitung selisih antara dua tanggal

from datetime import date

tgl1 = date(year = 1945, month = 8, day = 17)
tgl2 = date.today()
selisih = tgl2 - tgl1
print('Indonesia sudah merdeka selama =', selisih.days, ' hari')

catatan: selisih pada contoh di atas bertipe timedelta

Format tanggal dan waktu dengan strftime

from datetime import datetime

saat_ini = datetime.now()
jam = saat_ini.strftime('%H:%M:%S')
print('Jam:', jam)

tgl = saat_ini.strftime('%d/%m/%Y') # format dd/mm/YY
print('Tanggal:', tgl)

tgl_jam = saat_ini.strftime("%d/%m/%Y, %H:%M:%S") # format dd/mm/YY H:M:S 
print('tanggal dan jam: ', tgl_jam)

Konversi datetime dari dan ke format str(teks)

Untuk melakukan konversi sebuah nilai datetime dari dan ke nilai str (teks), modul datetime menyediakan fungsi strptime() dan strftime(). Contoh cara penggunaannya dibahas secara detail pada artikel ini.

I've tried to cover the key 
3
50 functions and methods here. You should definitely take a look at the documentation to read about the functions not covered in the tutorial. If you have any questions, feel free to let me know in the comments.

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to convert Year/Month/Day to Day of Year.
Next: Write a Python program to get week number.

What is the difficulty level of this exercise?

Easy Medium Hard

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Memory Management:

getrefcount will show how many times an object is used in the memory. It's a fantastic tool that can be used for memory management in any program and it's very convenient too.

Getrefcount will calculate the object usage at a low level ByteCode so it can tend to be higher than expected. For instance when you print a value that value is actually processed multiple times in the background inside the print function itself and getrefcount also counts the instance when the value is called with getrefcount method itself. So, it's safe to say that the count will actually always be at least 1 time higher than expected.

In this article, we will discuss the various way to retrieve the current time in milliseconds in python.

Using time.time() method

The time module in python provides various methods and functions related to time. Here we use the time.time() method to get the current CPU time in seconds. The time is calculated since the epoch. It returns a floating-point number expressed in seconds. And then, this value is multiplied by 1000 and rounded off with the round() function.

NOTE : Epoch is the starting point of time and is platform-dependent. The epoch is January 1, 1970, 00:00:00 (UTC) on Windows and most Unix systems, and leap seconds are not included in the time in seconds since the epoch.

We use time.gmtime(0) to get the epoch on a given platform.

Syntax

The syntax of time() method is as follows −

time.time()

Returns a float value that represents the seconds since the epoch.

Example

In the following example code, we get the current time in milliseconds by using different functions that are provided by the python datetime module.