Open Pin-Jiun opened 1 year ago
from datetime import time
# calling the constructor
my_time = time(12, 14, 36)
print("Entered time", my_time)
# calling constructor with 1
# argument
my_time = time(minute = 12)
print("\nTime with one argument", my_time)
# Calling constructor with
# 0 argument
my_time = time()
print("\nTime without argument", my_time)
# Uncommenting time(hour = 26)
# will rase an ValueError as
# it is out of range
# uncommenting time(hour ='23')
# will raise TypeError as
# string is passed instead of int
from datetime import time
# Getting min time
mintime = time.min
print("Min Time supported", mintime)
# Getting max time
maxtime = time.max
print("Max Time supported", maxtime)
from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
# Accessing Attributes
print("Hour:", Time.hour)
print("Minutes:", Time.minute)
print("Seconds:", Time.second)
print("Microseconds:", Time.microsecond)
<html>
<body>
<!--StartFragment-->
Function Name | Description
-- | --
dst() | Returns tzinfo.dst() is tzinfo is not None
fromisoformat() | Returns a time object from the string representation of the time
isoformat() | Returns the string representation of time from the time object
replace() | Changes the value of the time object with the given parameter
strftime() | Returns a string representation of the time with the given format
tzname() | Returns tzinfo.tzname() is tzinfo is not None
utcoffset() | Returns tzinfo.utcffsets() is tzinfo is not None
<!--EndFragment-->
</body>
</html>
from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
# Converting Time object to string
Str = Time.isoformat()
print("String Representation:", Str)
print(type(Str))
Time = "12:24:36.001212"
# Converting string to Time object
Time = time.fromisoformat(Str)
print("\nTime from String", Time)
print(type(Time))
from datetime import time
# Creating Time object
Time = time(12,24,36,1212)
print("Original time:", Time)
# Replacing hour
Time = Time.replace(hour = 13, second = 12)
print("New Time:", Time)
# Formatting Time
Ftime = Time.strftime("%I:%M %p")
print("Formatted time", Ftime)
class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)
# Python program to
# demonstrate datetime object
from datetime import datetime
# Initializing constructor
a = datetime(2022, 10, 22)
print(a)
# Initializing constructor
# with time parameters as well
a = datetime(2022, 10, 22, 6, 2, 32, 5456)
print(a)
``
___________
### Class Attributes
<html>
<body>
<!--StartFragment-->
Attribute Name | Description
-- | --
min | The minimum representable DateTime
max | The maximum representable DateTime
resolution | The minimum possible difference between datetime objects
year | The range of year must be between MINYEAR and MAXYEAR
month | The range of month must be between 1 and 12
day | The range of day must be between 1 and number of days in the given month of the given year
hour | The range of hour must be between 0 and 24 (not including 24)
minute | The range of minute must be between 0 and 60 (not including 60)
second | The range of second must be between 0 and 60 (not including 60)
microsecond | The range of microsecond must be between 0 and 1000000 (not including 1000000)
tzinfo | The object containing timezone information
fold | Represents if the fold has occurred in the time or not
<!--EndFragment-->
</body>
</html>
```python
from datetime import datetime
# Getting Today's Datetime
today = datetime.now()
# Accessing Attributes
print("Day: ", today.day)
print("Month: ", today.month)
print("Year: ", today.year)
print("Hour: ", today.hour)
print("Minute: ", today.minute)
print("Second: ", today.second)
import datetime
date_time_str = '2018-06-29 08:15:27.243860'
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S.%f')
print('Date:', date_time_obj.date())
print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
from datetime import datetime
now = datetime.now() # current date and time
year = now.strftime("%Y")
print("year:", year)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%d")
print("day:", day)
time = now.strftime("%H:%M:%S")
print("time:", time)
date_time = now.strftime("%m/%d/%Y, %H:%M:%S")
print("date and time:",date_time)
datetime
Python Datetime module comes built into Python, so there is no need to install it externally
Date and datetime are an object in Python, so when you manipulate them, you are actually manipulating objects and not string or timestamps.
The DateTime module is categorized into 6 main classes –
Date class
it represents a date in the format YYYY-MM-DD. Constructor of this class needs three mandatory arguments year, month and date.
Constructor syntax:
Get Today’s Year, Month, and Date
Timestamp
We can create date objects from timestamps y=using the fromtimestamp() method. The timestamp is the number of seconds from 1st January 1970 at UTC to a particular date.
Convert Date to String
We can convert date object to a string representation using two functions isoformat() and strftime().
其他常見的method
Function Name | Description -- | -- ctime() | Return a string representing the date fromisocalendar() | Returns a date corresponding to the ISO calendar fromisoformat() | Returns a date object from the string representation of the date fromordinal() | Returns a date object from the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1 fromtimestamp() | Returns a date object from the POSIX timestamp isocalendar() | Returns a tuple year, week, and weekday isoformat() | Returns the string representation of the date isoweekday() | Returns the day of the week as integer where Monday is 1 and Sunday is 7 replace() | Changes the value of the date object with the given parameter strftime() | Returns a string representation of the date with the given format timetuple() | Returns an object of type time.struct_time today() | Returns the current local date toordinal() | Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1 weekday() | Returns the day of the week as integer where Monday is 0 and Sunday is 6參考網址 https://www.geeksforgeeks.org/python-datetime-module/