0

I have a netcdf file that gives me a time value of this:

numpy.datetime64('2019-10-28T18:00:00.000000000')

How can I change it into this "28 Oct 2019 18Z" and add it into a plt.title?

levi
  • 1
  • 1
  • 3
    Does this answer your question? [Convert numpy.datetime64 to string object in python](https://stackoverflow.com/questions/19502506/convert-numpy-datetime64-to-string-object-in-python) – DeGo Jan 22 '22 at 14:16

1 Answers1

0
from datetime import datetime
import matplotlib.pyplot as plt

"""Convert datetime"""
timestring = '2019-10-28T18:00:00.000000000'[:26] #to make ist recognizable by strptime

new_dt_string = (datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S.%f'))
plt_dt_string = (datetime.strftime(new_dt_string, '%d %b %Y %HZ'))
print(plt_dt_string)
# output: 28 Oct 2019 18Z


"""Set plt title"""
plt.title('{}'.format(plt_dt_string))
ktw
  • 98
  • 9