“Holen Sie sich Datum und Uhrzeit in Python” Code-Antworten

Python aktuelles Datum

from datetime import date

today = date.today()
print("Today's date:", today)
MzanziLegend

Erhalten Sie das aktuelle Datum und die aktuelle Uhrzeit mit Python

import datetime
print(datetime.datetime.now())
2021-11-13 23:30:38.419951
print(datetime.date.today())
2021-11-13
David Cao

Holen Sie sich Datum und Uhrzeit in Python

from datetime import datetime

now = datetime.now().time().strftime("%H:%M:%S") # time object
date = datetime.now().strftime("%Y-%m-%d") # date object
print("date:",date)
print("time =", now)
Error451

Datum und Uhrzeit in Python

# You can use Numpy's datetime_as_string function.
# The unit='D' argument specifies the precision, in this case days.

t = numpy.datetime64('2012-06-30T20:00:00.000000000-0400')
numpy.datetime_as_string(t, unit='D')

Mckynde

Datum und Uhrzeit in Python

>>> import pytz
>>> from datetime import datetime, date, time
# To get the UTC time of current day (today) for a given time zone
# use the localize() function of the pytz. 
# The reason for this is that the localize() takes into account the Day Time Saving
# which niether the datetime constructors nor replace() account for.
# 
# To for example, the utc time of today in australia time zone:

import pytz
from datetime import datetime, date, time

# set time zone
tz = pytz.timezone("Australia/Melbourne")

# Get today's date in the current time zone (i.e local time)
todaydate = date.today() # you can also use date(2022, 2, 8)

# Get midnight of today (still local time)
# note time() without arguments will result to midnight

midnight_local = datetime.combine(todaydate, time())

print midnight_local

# convert this midnight datetime to the midnight datetime
# in the given time zone. In this case autralia time zone
austra_midnight = tz.localize(midnight_local)

print austra_midnight

# convert the midnight time in australia time zone to UTC
midnight_austra_utc = austra_midnight.astimezone(pytz.uct)

print midnight_austra_utc



# Ref:
# https://stackoverflow.com/questions/373370/how-do-i-get-the-utc-time-of-midnight-for-a-given-timezone
Mckynde

Ähnliche Antworten wie “Holen Sie sich Datum und Uhrzeit in Python”

Fragen ähnlich wie “Holen Sie sich Datum und Uhrzeit in Python”

Weitere verwandte Antworten zu “Holen Sie sich Datum und Uhrzeit in Python” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen