在Python中,我们可以使用time
模块来处理与时间相关的操作。time
模块提供了一些实用的函数,如获取当前时间、计算时间差、格式化时间等,要引入time
模块,只需在代码中使用import
关键字即可。
以下是一些常见的time
模块函数及其用法:
1、获取当前时间:
import time current_time = time.time() print("当前时间戳:", current_time)
2、将时间戳转换为可读的时间格式:
import time timestamp = time.time() readable_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime(timestamp)) print("可读时间:", readable_time)
3、计算两个时间戳之间的差值:
import time timestamp1 = time.time() 执行一些操作... timestamp2 = time.time() time_difference = timestamp2 timestamp1 print("时间差(秒):", time_difference)
4、休眠一段时间:
import time print("开始休眠") time.sleep(5) # 休眠5秒 print("休眠结束")
5、格式化时间:
import time from datetime import datetime, timedelta now = datetime.now() yesterday = now timedelta(days=1) tomorrow = now + timedelta(days=1) formatted_now = now.strftime("%Y%m%d %H:%M:%S") formatted_yesterday = yesterday.strftime("%Y%m%d %H:%M:%S") formatted_tomorrow = tomorrow.strftime("%Y%m%d %H:%M:%S") print("今天的日期和时间:", formatted_now) print("昨天的日期和时间:", formatted_yesterday) print("明天的日期和时间:", formatted_tomorrow)
6、获取当前日期和星期几:
import time from datetime import date, timedelta, weekday now = date.today() weekday_number = now.weekday() + 1 # 星期一为0,星期二为1,以此类推...星期天为6 weekday_name = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"][weekday_number 1] print("今天的日期:", now) print("今天是星期:", weekday_name)
7、获取当前年份、月份和日期:
import time from datetime import date, month, year, day, weekday, timedelta, tzinfo, timezone, DSTInfo, localize, mktime, asctime, ctime, gmtime, mktime_ns, asctime_ns, ctime_ns, gmtime_ns, strftime, strptime, altzone, utcfromtimestamp, utcoffset, tzname, gettz, daylight, dst, fromordinal, isocalendar, isoweekday, weekday, replace, yearmonthrange, localdate, min, max, timetuple_ns, pytz, combine, date as parse_date, grange, period_range, periods_between, relativedelta as relativedelta_local, ArrowInvalid as arrowinvalid, ArrowInvalidTypeError as arrowinvalidtypeerror, ArrowNotImplemented as arrownotimplemented, ArrowNaT as arrowna, ArrowOutOfBoundsDateError as arrowoutofboundsdateerror, ArrowInvalidTimeError as arrowinvalidtimeerror, ArrowInvalidDateError as arrowinvaliddateerror, ArrowInvalidRangeError as arrowinvalidrangeerror, ArrowUnknownTimeZoneError as arrowunknowntimezoneerror, ArrowDatabaseError as arrowdatabaseerror, ArrowDataError as arrowdataerror, ArrowNotSupportedError as arrownotsupportederror, ArrowOperationNotSupportedError as arrowoperationnotsupportederror, ArrowDeprecationWarning as arrowdeprecationwarning, ArrowWarning as arrowwarning, ArrowTypeError as arrowtypeerror, ArrowIndexError as arrowindexerror, ArrowOverflowError as arrowoverflowerror, ArrowMemoryError as arrowmemoryerror, ArrowSystemError as arrowsystemerror, ArrowIOError as arrowioerror, ArrowTimeoutError as arrowtimeouterror, ArrowRuntimeError as arrowruntimeerror, ArrowRecursionError as arrowrecursionerror, ArrowMaxRecursionDepthReachedError as arrowmaxrecursiondepthreachederror, ArrowPrecisionLossError as arrowprecisionlosserror, ArrowFloatingPointError as arrowfloatingpointerror, ArrowDecimalException as arrowdecimalexception, ArrowInvalidUTF8Error as arrowinvalidutf8error, ArrowUnicodeEncodeError as arrowunicodeencodeerror, ArrowUnicodeDecodeError as arrowunicodedecodeerror, ArrowUnicodeError as arrowunicodeerror, ArrowJSONDecodeError as arrowjsondecodeerror, ArrowJSONEncodeError as arrowjsonencodeerror, ArrowParseError as arrowparseerror, ArrowTableDeleteError as arrowtabledeleteerror, ArrowTableWriteError as arrowtablewriteerror, ArrowTableReadError as arrowtablereaderror, ArrowTableCreateError as arrowtablecreateerror, ArrowSchemaMismatchError as arrowschemamismatcherror, ArrowKeySizeMismatchError as arrowkeysizemismatcherror, ArrowDataTypeMismatchError as arrowdatatypemismatcherror, ArrowConvertOutputTypeError as arrowconvertoutputtypeerror, ArrowInvalidDictionaryKeyError as arrowinvaliddictionarykeyerror, ArrowDuplicateKeyError as arrowduplicatekeyerror, ArrowMissingKeyError as arrowmissingkeyerror, ArrowBufferTooSmallError as arrowbuffertoosmallerror, ArrowUnknownFileFormatError as arrowunknownfileformaterror, ArrowFileNotFoundError as arrowfilenotfounderror, ArrowFileAlreadyExistsError as arrowfilealreadyexistserror, ArrowFileOpenFailedError as arrowfileopenfailederror, ArrowFileWriteFailedError as arrowfilewritefailederror, ArrowFileReadFailedError as arrowfilereadfailederror, g for struct_arrow in zip((pytz.all_timezones), range(len(pytz.all_timezones)))] print("当前的日期是:", now) print("今天是星期几:", weekday_name) print("今年是哪一年:", year) print("今年的第几天:", day) or tzinfo是时区信息类,用于表示不同时区的时间和日期; tzname是一个包含所有有效时区名称的元组; tz是datetime模块中的一个属性,它是一个映射对象,将字符串形式的时区名称映射到相应的tzinfo对象; dt是datetime模块中的一个类,用于表示一个日期和时间; dtinfo是一个抽象基类,用于表示具有时区信息的日期和时间。
最新评论
本站CDN与莫名CDN同款、亚太CDN、速度还不错,值得推荐。
感谢推荐我们公司产品、有什么活动会第一时间公布!
我在用这类站群服务器、还可以. 用很多年了。