Convert time to datetime python

In this article, you will learn to convert timestamp to datetime object and datetime object to timestamp (with the help of examples).

It's pretty common to store date and time as a timestamp in a database. A Unix timestamp is the number of seconds between a particular date and January 1, 1970 at UTC.


Example 1: Python timestamp to datetime

from datetime import datetime

timestamp = 1545730073
dt_object = datetime.fromtimestamp(timestamp)

print("dt_object =", dt_object)
print("type(dt_object) =", type(dt_object))

When you run the program, the output will be:

dt_object = 2018-12-25 09:27:53
type(dt_object) = 

Here, we have imported datetime class from the datetime module. Then, we used datetime.fromtimestamp() classmethod which returns the local date and time (datetime object). This object is stored in dt_object variable.

Note: You can easily create a string representing date and time from a datetime object using strftime() method.


Example 2: Python datetime to timestamp

You can get timestamp from a datetime object using datetime.timestamp() method.

from datetime import datetime

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

timestamp = datetime.timestamp(now)
print("timestamp =", timestamp)

from datetime import datetime
from time import time

print(f'{datetime.now()} and {time()}')

Yield 2022-03-25 16:27:37.051854 and 1648193257.051854
I hope to getdatetime.now() from time().

I found Sec: 37 = 1648193257 % 60.
27469887 = 1648193257 // 60
min: 27 = 27469887 % 60

But I could not find how can yield hour data and so on.

asked Mar 25 at 7:56

Convert time to datetime python

1

You can use the datetime.datetime.fromtimestamp() function for it.

print(datetime.datetime.now())
now = time.time()
print(now)
print(datetime.datetime.fromtimestamp(now))

Yields

2022-03-25 09:13:42.665469
1648196022.6654692
2022-03-25 09:13:42.665469

A similar question with answers you can find here

answered Mar 25 at 8:15

Convert time to datetime python

DolukDoluk

3361 silver badge8 bronze badges

I recommend that you review this documentation

As an example, you can get the current hour as follows:

import time

print(time.localtime().tm_hour)

answered Mar 25 at 8:15

Convert time to datetime python

VladVlad

10.7k2 gold badges5 silver badges17 bronze badges


In this article, we will discuss how to convert a date to a datetime object in python.

We use the combine method from the datetime module to combine a date and time object to create a datetime object.

Syntax

The syntax of combine() method is as follows.

datetime.combine(date,time)

Where,

  • date is a date object.
  • time is a time object.

This method returns a datetime object which is the combination of the above two objects(date,time).

Converting a date object to datetime object

If we have a date object and don't have a time object, then you initialize the time object to a minimum using the datetime object (Minimum means at midnight i.e 00:00:00).

Example

In the below example code, we combine a date object is retrieved by using the today() method and we initialized the time object to minimum time(00:00:00) using the min.time() method. And combined these two objects by applying the datetime.combine() method.

from datetime import date from datetime import datetime my_date = date.today() print("The date object:",my_date) my_time = datetime.min.time() print("The time object:",my_time) my_datetime = datetime.combine(my_date, my_time) print("The combined datetime object:",my_datetime)

Output

The output of the above code is as follows.

The date object: 2022-05-19
The time object: 00:00:00
The combined datetime object: 2022-05-19 00:00:00

When both date and time objects are given

Here, we initialize both date and time objects. And combine these into a datetime object using the combine method.

Example

In this example, we will convert a datetime object when both date and time objects are given.

import datetime my_date = datetime.datetime(2012, 2, 12) print("The date object:",my_date) my_time = datetime.time(1, 30) print("The time object:",my_time) combined_datetime = my_date.combine(my_date, my_time) print("The combined datetime object:",combined_datetime)

Output

The output of the above code is as follows;

The date object: 2012-02-12 00:00:00
The time object: 01:30:00
The combined datetime object: 2012-02-12 01:30:00

Using the datetime()

The datetime() constructor in python accepts year, month and date values as parameters and create a datetime object.

Pass the year, month and day values of the desired date object (as my_date.year, my_date.month, my_date.day) to this constructor to convert a date object to datetime object.

Example

from datetime import date from datetime import datetime my_date = date.today() my_datetime = datetime(my_date.year, my_date.month, my_date.day) print(my_datetime)

Output

2022-09-05 00:00:00

Convert time to datetime python

Updated on 08-Sep-2022 06:58:03

  • Related Questions & Answers
  • How to convert Python date string mm/dd/yyyy to datetime?
  • How to convert JS date time to MySQL datetime?
  • MySQL Query to convert from datetime to date?
  • How to convert Python datetime to epoch with strftime?
  • How to convert timestamp string to datetime object in Python?
  • How to convert Python DateTime string into integer milliseconds?
  • How to convert timestamp to datetime in MySQL?
  • How to convert TimeStamp to DateTime in Kotlin?
  • How to convert pandas offset to Python date?
  • How to convert Python date to Unix timestamp?
  • How to convert a Python date string to a date object?
  • How to convert a JS Date to a Python date object?
  • How to convert Python date in JSON format?
  • How to convert MySQL datetime to Unix timestamp?
  • Convert string to DateTime and vice-versa in Python

How do I convert time to string in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert a date to datetime?

combine() method..
from datetime import date from datetime import datetime my_date = date. today() print("The date object:",my_date) my_time = datetime. ... .
import datetime my_date = datetime. ... .
from datetime import date from datetime import datetime my_date = date..

How do I convert a timestamp?

The UNIX timestamp is a way to track time as a running total of seconds. ... Convert Timestamp to Date..

What is Fromtimestamp in Python?

Example 1: Python timestamp to datetime The fromtimestamp() function is used to return the date associated with a given timestamp. The date class's function fromtimestamp() computes and returns the date and time corresponding to a specified timestamp. The timestamps in this example range from 1970 to 2038.