How to convert NumPy datetime64 to Timestamp?

02 Sep, 2020
In this article, we will discuss how to convert NumPy datetime64 to Timestamp. For better understanding first, let’s see the below example: 
If the current datetime64 is as follows: 2020-08-18 09:31:51.944622
then,
the required timestamp in seconds will be: 1597743111.944622
the required timestamp in minutes will be: 26629051.8657437
the required timestamp in an hour will be: 443817.53109572834

Approach:
Taking date through np.datetime64().
Subtracting that date from default system date that is 1970-01-01T00:00:00Z.
With the help of np.timedelta64() converting the date into seconds, minutes and hour.
Example 1:
Python3
# importing required libraryimport numpy as npfrom datetime import datetime  # extracting current date # in utc formatdate = datetime.utcnow()  print("Printing the Current date:",      date)  # converting the current date# in datetime64 formatdate64 = np.datetime64(date)  # converting date time into second timestamp ts = (date64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')  print("Printing the converted datetime in Timestamp in seconds:",     ts)  # converting date time into minute timestamp tm = (date64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'm')  print("Printing the converted datetime in Timestamp in minutes:",     ts)  # converting date time into hour timestamp th = (date64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'h')  print("Printing the converted datetime in Timestamp in hour:",      th)
Output:
Printing the Current date: 2020-08-26 10:07:54.820878Printing the converted datetime in Timestamp in seconds: 1598436474.820878Printing the converted datetime in Timestamp in minutes: 1598436474.820878Printing the converted datetime in Timestamp in hour: 444010.13189468835
Example2: Converting 15 Aug 2020 in seconds, minutes and hour timestamp: 
Python3
#importing required libraryimport numpy as npfrom datetime import datetime  print("Printing the date:")  # extracting current date in utc formatdt64 = np.datetime64('2020-08-15');  print(dt64)  # converting date time into second timestamp ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')print("Printing the converted datetime in Timestamp in seconds:",       ts)print("Printing the converted datetime in Timestamp in minutes")  # converting date time into minute timestamp tm = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'm')print(tm)  print("Printing the converted datetime in Timestamp in hour")  # converting date time into hour timestamp th = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'h')print(th)
Output:
Printing the date:2020-08-15Printing the converted datetime in Timestamp in seconds:1597449600.0Printing the converted datetime in Timestamp in minutes26624160.0Printing the converted datetime in Timestamp in hour443736.0
Python numpy-DataType
Python-numpy
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python