📜  PythonDatetime.date类的isoformat()函数

📅  最后修改于: 2022-05-13 01:55:28.211000             🧑  作者: Mango

PythonDatetime.date类的isoformat()函数

Isoformat()函数用于以ISO 8601格式返回对应时区的日期、时间和 UTC 偏移量的字符串。

标准 ISO 8601 格式是关于公历的日期格式。此格式规定日历日期需要使用 4 位年份后跟两位数月份和两位数日期来表示。即,YYYY-MM-DD。示例:2020-01-01。

示例 1:在下面的示例中,isoformat()函数在今天的日期被调用,它以 ISO 8601 格式返回相同的今天的日期字符串。

Python3
# Python3 code to demonstrate
# Getting date values in ISO 8601 format
  
# importing datetime and time module
import datetime
import time
          
# Getting today's date 
todays_Date = datetime.date.fromtimestamp(time.time());
  
# Calling the isoformat() function over the
# today's date
date_in_ISOFormat = todays_Date.isoformat();
  
# Printing Today's date in ISO format
print("Today's date in ISO Format: %s"%date_in_ISOFormat);


Python3
# Python3 code to demonstrate
# Getting date and time values 
# in ISO 8601 format
  
# importing datetime and time module
import datetime
import time
      
# Getting today's date and time
todays_Date = datetime.datetime.now(); 
  
# Calling the isoformat() function over the
# today's date and time
DateTime_in_ISOFormat = todays_Date.isoformat();
  
# Printing Today's date and time in ISO format
print("Today's date and time in ISO Format: %s"%DateTime_in_ISOFormat);


Python3
# Python3 code to demonstrate
# Getting date and time values 
# in ISO 8601 format
  
# importing datetime module
import datetime
      
# Getting today's date and time
DateTime_in_ISOFormat = datetime.datetime.now(); 
  
# Printing Today's date and time in ISO format of
# auto value for the format specifier
print(DateTime_in_ISOFormat.isoformat("#", "auto"))
  
# Printing Today's date and
# time format specifier as hours
print(DateTime_in_ISOFormat.isoformat("#", "hours"))
  
# Printing Today's date and
# time format specifier as minutes
print(DateTime_in_ISOFormat.isoformat("#", "minutes"))
  
# Printing Today's date and time
# format specifier as seconds
print(DateTime_in_ISOFormat.isoformat("#", "seconds"))
  
# Printing Today's date and time
# format specifier as milliseconds
print(DateTime_in_ISOFormat.isoformat("#", "milliseconds"))
  
# Printing Today's date and time
# format specifier as microseconds
print(DateTime_in_ISOFormat.isoformat("#", "microseconds"))


输出:

Today's date in ISO Format: 2021-07-27

示例 2:在下面的示例中,isoformat()函数在今天的日期和时间被调用,它以 ISO 8601 格式返回相同的今天的日期和时间字符串。

蟒蛇3

# Python3 code to demonstrate
# Getting date and time values 
# in ISO 8601 format
  
# importing datetime and time module
import datetime
import time
      
# Getting today's date and time
todays_Date = datetime.datetime.now(); 
  
# Calling the isoformat() function over the
# today's date and time
DateTime_in_ISOFormat = todays_Date.isoformat();
  
# Printing Today's date and time in ISO format
print("Today's date and time in ISO Format: %s"%DateTime_in_ISOFormat);

输出:

示例 3:在下面的示例中,isoformat()函数采用了两个参数,一个是分隔字符,例如“#”,另一个参数是特定时间的格式说明符。此处使用的时间特定参数的不同值已在上述参数部分中说明。

蟒蛇3

# Python3 code to demonstrate
# Getting date and time values 
# in ISO 8601 format
  
# importing datetime module
import datetime
      
# Getting today's date and time
DateTime_in_ISOFormat = datetime.datetime.now(); 
  
# Printing Today's date and time in ISO format of
# auto value for the format specifier
print(DateTime_in_ISOFormat.isoformat("#", "auto"))
  
# Printing Today's date and
# time format specifier as hours
print(DateTime_in_ISOFormat.isoformat("#", "hours"))
  
# Printing Today's date and
# time format specifier as minutes
print(DateTime_in_ISOFormat.isoformat("#", "minutes"))
  
# Printing Today's date and time
# format specifier as seconds
print(DateTime_in_ISOFormat.isoformat("#", "seconds"))
  
# Printing Today's date and time
# format specifier as milliseconds
print(DateTime_in_ISOFormat.isoformat("#", "milliseconds"))
  
# Printing Today's date and time
# format specifier as microseconds
print(DateTime_in_ISOFormat.isoformat("#", "microseconds"))

输出:

2021-07-27#16:01:12.090202
2021-07-27#16
2021-07-27#16:01
2021-07-27#16:01:12
2021-07-27#16:01:12.090
2021-07-27#16:01:12.090202