Datetime Utc Python

Python Dates

  1. Python Datetime From Utc
  2. Python Datetime Convert To Utc
  3. Datetime Format Python
  4. Get Utc Time Python

A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC. Example 1: Python timestamp to datetime. Python Server Side Programming Programming You can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp to get a UTC timestamp. This function returns the time since epoch for that datetime object.

  1. Module: datetime. Provides the time and datetime types with which the ZoneInfo class is designed to be used. First-party package maintained by the CPython core developers to supply time zone data via PyPI.
  2. Use the pytz module, which comes with a full list of time zones + UTC. Figure out what the local timezone is, construct a timezone object from it, and manipulate and attach it to the naive datetime. Finally, use datetime.astimezone method to convert the datetime to UTC.

A date in Python is not a data type of its own, but we can import a module named datetime to work with dates as date objects.


Import the datetime module and display the current date:

import datetime
x =
Try it Yourself »

Date Output

Datetime to utc python

When we execute the code from the example above the result will be:

The date contains year, month, day, hour, minute, second, and microsecond.

The datetime module has many methods to return information about the date object.

Here are a few examples, you will learn more about them later in this chapter:


Return the year and name of weekday:

import datetime
x =
Try it Yourself »

Creating Date Objects

To create a date, we can use the datetime() class (constructor) of the datetime module.

The datetime() class requires three parameters to create a date: year, month, day.


Datetime Utc Python

Create a date object:

import datetime
x = datetime.datetime(2020, 5, 17)
DatetimeUtcTry it Yourself »

The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone).

Amazon prime photos sign in

The strftime() Method

The datetime object has a method for formatting date objects into readable strings.

The method is called strftime(), and takes one parameter, format, to specify the format of the returned string:


Display the name of the month:

Python Datetime From Utc

Python datetime module
import datetime
x = datetime.datetime(2018, 6, 1)
Try it Yourself »

Python Datetime Convert To Utc

A reference of all the legal format codes:

Datetime Format Python

DirectiveDescriptionExampleTry it
%aWeekday, short versionWedTry it »
%AWeekday, full versionWednesdayTry it »
%wWeekday as a number 0-6, 0 is Sunday3Try it »
%dDay of month 01-3131Try it »
%bMonth name, short versionDecTry it »
%BMonth name, full versionDecemberTry it »
%mMonth as a number 01-1212Try it »
%yYear, short version, without century18Try it »
%YYear, full version2018Try it »
%HHour 00-2317Try it »
%IHour 00-1205Try it »
%pAM/PMPMTry it »
%MMinute 00-5941Try it »
%SSecond 00-5908Try it »
%fMicrosecond 000000-999999548513Try it »
%zUTC offset+0100
%jDay number of year 001-366365Try it »
%UWeek number of year, Sunday as the first day of week, 00-5352Try it »
%WWeek number of year, Monday as the first day of week, 00-5352Try it »
%cLocal version of date and timeMon Dec 31 17:41:00 2018Try it »
%xLocal version of date12/31/18Try it »
%XLocal version of time17:41:00Try it »
%%A % character%Try it »
%GISO 8601 year2018Try it »
%uISO 8601 weekday (1-7)1Try it »
%VISO 8601 weeknumber (01-53)01Try it »

Get Utc Time Python