• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Convert Timestamp to Date

Using Python to Convert Timestamp to Date

August 26, 2022 Leave a Comment

To convert a timestamp to a date in Python, the easiest way is to use the datetime fromtimestamp() function to create a datetime object and then convert that to a date.

from datetime import datetime 

ts = 1661540168

dt = datetime.fromtimestamp(ts).date()

print(dt)

#Output:
2022-08-26

When working with date and times, the ability to change between different variable types easily can be valuable.

One such case is if you want to convert a timestamp to a date.

To convert a timestamp to a date in Python, the easiest way is to use the datetime fromtimestamp() function to create a datetime object.

After you have the datetime object, then you can remove the time from the datetime and get the date with date().

Below is a simple example showing you how to convert a timestamp to a date in Python

from datetime import datetime 

ts = 1661540168

dt = datetime.fromtimestamp(ts).date()

print(dt)

#Output:
2022-08-26

Convert Timestamp to Date with Timezone in Python

If the timestamp you are using needs to be converted to a date with a specific timezone, then you can specify a timezone with the optional tz argument for fromtimestamp().

Below is an example of converting a timestamp to a date object and specifying a timezone.

from datetime import datetime 
import pytz

ts = 1661540168

dt = datetime.fromtimestamp(ts, tz=pytz.utc).date()

print(dt)

#Output:
2022-08-26

Hopefully this article has been useful for you to learn how to convert a timestamp into a date using Python.

Other Articles You'll Also Like:

  • 1.  pandas covariance – Calculate Covariance Matrix Using cov() Function
  • 2.  Using Python to Check if String Contains Only Letters
  • 3.  Using Python to Remove Commas from String
  • 4.  How to Sort Numbers in Python Without Sort Function
  • 5.  Find Last Occurrence in String of Character or Substring in Python
  • 6.  Check if Character is Letter in Python
  • 7.  Using Python to Check If Variable is Not None
  • 8.  pandas round – Round Numbers in Series or DataFrame
  • 9.  Python Check if Attribute Exists in Object with hasattr() Function
  • 10.  Get Days of timedelta Object in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy