Cara menggunakan python datetime with gmt

There are many ways to get the current date and time in Python using the built-in and third-party modules. The below steps show how to get the current date and time using the datetime and time module.

  1. Import datetime module

    Python’s datetime module provides functions that handle many complex functionalities involving the date and time. Import the

    YYYY-MM-DD HH:MM:SS.MS
    3 class using a
    YYYY-MM-DD HH:MM:SS.MS
    4 statement.

  2. Use the now() function of a datetime class

    The

    YYYY-MM-DD HH:MM:SS.MS
    5 returns the current local date and time. By default, it represents datetime in
    YYYY-MM-DD HH:MM:SS.MS
    6 format. Note: The date and time values are stored as
    YYYY-MM-DD HH:MM:SS.MS
    3 objects, The
    YYYY-MM-DD HH:MM:SS.MS
    3 object represents both date and time

  3. Use the today() function of a Date class

    Use this step if you want only the current date and not the time. The

    YYYY-MM-DD HH:MM:SS.MS
    9 method of a date class returns the current local date

  4. Use time module

    Use the

    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    0 function to get the current time in seconds since the epoch as a floating-point number

Example: Get Current DateTime in Python

from datetime import datetime

now = datetime.now()
print('Current DateTime:', now)
print('Type:', type(now))

Output:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>

As you can see in the output we got the current date and time in the following format.

YYYY-MM-DD HH:MM:SS.MS

Refer to Python DateTime Format Using Strftime() If you want to get the current date in various formats.

Extract Current Date and Time Separately from a Datetime Object

Note: in Python, The date and datetime are objects. So when we are manipulating date and time, that means we are actually dealing with objects.

For example, you can extract the current date and time separately from a

YYYY-MM-DD HH:MM:SS.MS
3 object

  • Use the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    2 function to get the date in
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    3 format
  • Use the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    4 function to get the time in the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    5 format.
# import only datetime class
from datetime import datetime

# current datetime
now = datetime.now()

current_date = now.date()
print('Date:', current_date)
print(type(current_date))

current_time = now.time()
print('Time', current_time)
print(type(current_time))

Output:

Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>

Break DateTime to Get Current Year, Month, Day, Hour, Minute, Seconds

The datetime module provides several attributes to access the induvial component such as a year, month, day, hour, minute, seconds.

Example:

In this example, we’ll break the current datetime and assign them into variables like the year, month, day, hour, minute, seconds, and microseconds.

from datetime import datetime

# Get current date and time
now = datetime.now()

# extract attributes 
print("Year:", now.year)
print("Month:", now.month)
print("Day =", now.day)

print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
print("Microsecond:", now.microsecond)

Output:

Year: 2021
Month: 7
Day = 16
Hour: 8
Minute: 28
Second: 0
Microsecond: 619640

Note: You can also use the

# import only datetime class
from datetime import datetime

# current datetime
now = datetime.now()

current_date = now.date()
print('Date:', current_date)
print(type(current_date))

current_time = now.time()
print('Time', current_time)
print(type(current_time))
6 to break the datetime an get the induvial attribute for it.

Get Current Date using the Date class

Python Datetime module provides the Date class to represent and manipulate the dates. The Date class considers the Gregorian calendar.

  • Import the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    7 class from the datetime module
  • Use the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    8 method to get the current date.

Example:

from datetime import date

today = date.today()
print('Current Date:', today)

# Output 2021-07-16

Note: The

# import only datetime class
from datetime import datetime

# current datetime
now = datetime.now()

current_date = now.date()
print('Date:', current_date)
print(type(current_date))

current_time = now.time()
print('Time', current_time)
print(type(current_time))
9 return the current date and time. This method is functionally equivalent to 
Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
0, but without timezone information.

Get Current Time in Python

There are many ways to get the current time in Python using the built-in and third-party modules. Python time module provides various functions to get the current time and perform time-related activities. We will see each one by one

Current Time in Seconds Using # import only datetime class from datetime import datetime # current datetime now = datetime.now() current_date = now.date() print('Date:', current_date) print(type(current_date)) current_time = now.time() print('Time', current_time) print(type(current_time))0

Use the

# import only datetime class
from datetime import datetime

# current datetime
now = datetime.now()

current_date = now.date()
print('Date:', current_date)
print(type(current_date))

current_time = now.time()
print('Time', current_time)
print(type(current_time))
0 function to get the current time in seconds since the epoch as a floating-point number.

This method returns the current timestamp in a floating-point number that represents the number of seconds since Jan 1, 1970, 00:00:00.

It returns the current time in

Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
3 format.

Example:

import time

# get current time in seconds
t = time.time()
print('Time:', t)

# Output 1626488018.0199707

You can use this timestamp to also.

Current Time in MiliSeconds Using

# import only datetime class
from datetime import datetime

# current datetime
now = datetime.now()

current_date = now.date()
print('Date:', current_date)
print(type(current_date))

current_time = now.time()
print('Time', current_time)
print(type(current_time))
0

Current Time Using Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>5

Use the

Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
5 function to display the current time in a human-readable format. This function represents the current time in the operating system preferred way. The output may vary as per the operating system.

Example:

import time

# get current time
print('Current Time:', time.ctime(time.time()))

# Output Sat Jul 17 07:07:09 2021

Current Time Using Date: 2021-07-16 <class 'datetime.date'> Time 08:25:05.282627 <class 'datetime.time'>7

Use the

Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
7 function to return the current time expressed in seconds since the epoch to a local time in the a 
Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
9 format.

You can access year, month, day, hour, minute, seconds, and microseconds from a

Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
9.

Example:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
0

Output:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
1

Get Current Time Using Datetime Module

The

YYYY-MM-DD HH:MM:SS.MS
5 method of a datetime class returns the current time in a human-readable format. It internally uses the
Date: 2021-07-16
<class 'datetime.date'>

Time 08:25:05.282627
<class 'datetime.time'>
7 without the timezone info (if not given).

Also, you can access the individual attribute such as hour, minutes, seconds, and microseconds

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
2

Output:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
3

Get Current Time in Milliseconds

There is no specific attribute or method in Python to get the current time in milliseconds. However, as milliseconds are three decimal places away from seconds, we can convert seconds to milliseconds by multiplying seconds by 1000.

  • Use the
    # import only datetime class
    from datetime import datetime
    
    # current datetime
    now = datetime.now()
    
    current_date = now.date()
    print('Date:', current_date)
    print(type(current_date))
    
    current_time = now.time()
    print('Time', current_time)
    print(type(current_time))
    0 to get the current time in seconds since the epoch as a floating point number
  • Multiply time by 1000 to get current time in milliseconds

Example:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
4

Get Current UTC Time

UTC – Coordinated Universal Time is the common time standard across the world. So, in Python, to work with the timezone without any issues, it is recommended to use the UTC as your base timezone.

  • Use the
    YYYY-MM-DD HH:MM:SS.MS
    5 method to get the current time
  • Use the timezone class with UTC instance with a
    Date: 2021-07-16
    <class 'datetime.date'>
    
    Time 08:25:05.282627
    <class 'datetime.time'>
    0 method to to get the current UTC time in Python

Example:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
5

Output:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
6

Get Current Time in a Specific Timezone

Use the third-party pytz module to get the current time of any timezone.

Steps:

  • Install pytz module using the
    from datetime import datetime
    
    # Get current date and time
    now = datetime.now()
    
    # extract attributes 
    print("Year:", now.year)
    print("Month:", now.month)
    print("Day =", now.day)
    
    print("Hour:", now.hour)
    print("Minute:", now.minute)
    print("Second:", now.second)
    print("Microsecond:", now.microsecond)
    6
  • Use the 
    from datetime import datetime
    
    # Get current date and time
    now = datetime.now()
    
    # extract attributes 
    print("Year:", now.year)
    print("Month:", now.month)
    print("Day =", now.day)
    
    print("Hour:", now.hour)
    print("Minute:", now.minute)
    print("Second:", now.second)
    print("Microsecond:", now.microsecond)
    7 function to create the timezone object
  • Use 
    from datetime import datetime
    
    # Get current date and time
    now = datetime.now()
    
    # extract attributes 
    print("Year:", now.year)
    print("Month:", now.month)
    print("Day =", now.day)
    
    print("Hour:", now.hour)
    print("Minute:", now.minute)
    print("Second:", now.second)
    print("Microsecond:", now.microsecond)
    8 function to get the current datetime with timezone

Example:

Refer to our guide on working with timezones in Python.

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
7

Output:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
8

Get Current GMT Time

Greenwich Mean Time or GMT is clock time at the Royal Observatory in Greenwich, London. It is not affected by Summer Time (Daylight Saving Time) clock changes.

GMT
  • Use the time.gmtime() method to get the current GMT time in Python
  • Pass the time in seconds to this method to get the GMT representation of a time

Example:

Current DateTime: 2021-07-16 19:17:20.536991
Type: <class 'datetime.datetime'>
9

Output:

YYYY-MM-DD HH:MM:SS.MS
0

Get Current Time in ISO Format

Use the

from datetime import datetime

# Get current date and time
now = datetime.now()

# extract attributes 
print("Year:", now.year)
print("Month:", now.month)
print("Day =", now.day)

print("Hour:", now.hour)
print("Minute:", now.minute)
print("Second:", now.second)
print("Microsecond:", now.microsecond)
9 method to get the current date and time in ISO format.

Use

Year: 2021
Month: 7
Day = 16
Hour: 8
Minute: 28
Second: 0
Microsecond: 619640
0 method on a
YYYY-MM-DD HH:MM:SS.MS
5 instance to get the current date and time in the following ISO 8601 format: