当前位置: 首页 > news >正文

做网站最好的语言网络广告的优势有哪些

做网站最好的语言,网络广告的优势有哪些,我做动作你来猜的网站,兖州住房与城乡建设局网站目录 前言 datetime模块 一、datetime 类 1.创建 datetime 对象 2.获取日期时间的各个部分 3.格式化日期时间为字符串 4.解析字符串为 datetime 对象 二、timedelta 类 1.创建 timedelta 对象 datetime注意事项 time模块 1.获取当前时间戳 2.获取当前时间的结构化表…

目录

前言

datetime模块 

一、datetime 类

1.创建 datetime 对象

2.获取日期时间的各个部分

3.格式化日期时间为字符串

4.解析字符串为 datetime 对象

二、timedelta 类

1.创建 timedelta 对象

datetime注意事项

  time模块

1.获取当前时间戳

2.获取当前时间的结构化表示

3.格式化时间

4.睡眠

time注意事项


前言

        在 Python 中,datetime 是处理日期和时间的核心模块,位于标准库中。它提供了用于创建、操作和格式化日期时间的类和函数。Python 的 time 模块是处理时间的另一个重要工具,与 datetime 模块不同,它主要用于处理时间戳(Unix 时间戳)、计时器功能和简单的时间操作。

 

datetime模块 

  • datetime 模块提供了用于操作日期和时间的类和函数,主要用于处理日期时间对象、日期算术运算、日期格式化等。
  • 主要的类包括 datetime 和 datetime。例如,datetime.datetime 类用于表示具体的日期和时间,包括年、月、日、时、分、秒和微秒。
  • datetime 模块中的函数和类能够处理较复杂的日期和时间操作,例如计算日期差异、时区转换等。

 

一、datetime 类

   datetime 类用于表示特定的日期和时间,包括年、月、日、时、分、秒等信息。它位于 datetime 模块中。

 

1.创建 datetime 对象

from datetime import datetime # 从datetime模块中导入datetime类# 创建一个当前日期时间的对象
now = datetime.now()
print(now)  # 创建一个指定日期时间的对象
specific_date = datetime(2023, 12, 31, 23, 59, 59)
print(specific_date)  

 输出:

2024-07-16 22:59:50.631692
2023-12-31 23:59:59

 

2.获取日期时间的各个部分

from datetime import datetime# 获取年、月、日、时、分、秒等部分
now = datetime.now()
year = now.year                # 获取年份
month = now.month              # 获取月份
day = now.day                  # 获取天数
hour = now.hour                # 获取小时
minute = now.minute            # 获取分钟
second = now.second            # 获取秒数
microsecond = now.microsecond  # 获取毫秒print(year, month, day, hour, minute, second, microsecond)

输出:

2024 7 16 23 1 40 665821

 

3.格式化日期时间为字符串

from datetime import datetime# 格式化日期时间为字符串
now = datetime.now()
formatted_date = now.strftime("%Y-%m-%d %H:%M:%S")
print(formatted_date,type(formatted_date))

输出:

2024-07-16 23:09:53 <class 'str'>

 

4.解析字符串为 datetime 对象

from datetime import datetime# 解析字符串为 datetime 对象
date_str = "2023-12-31 23:59:59"
parsed_date = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
print(parsed_date,type(parsed_date)) 

输出:

2023-12-31 23:59:59 <class 'datetime.datetime'>

 

 

二、timedelta 类

    timedelta 类用于表示两个日期时间之间的差异,或者一个时间段。它也位于 datetime 模块中。

 

1.创建 timedelta 对象

from datetime import datetime, timedelta# 创建一个时间差异对象
delta = timedelta(days=5, hours=3, minutes=30)
print(delta)  # 5 days, 3:30:00# 使用时间差异来进行日期计算
start_date = datetime(2024, 7, 1)
end_date = start_date + delta
print(end_date)  # 2024-07-06 03:30:00

输出:

5 days, 3:30:00
2024-07-06 03:30:00

 

datetime注意事项

  • datetime 对象是不可变的,一旦创建就不能修改其值。
  • datetime 模块中的函数和类提供了强大的日期时间处理功能,能够处理日期时间的格式化、比较、计算等各种操作。

        使用 datetime 模块可以方便地在 Python 中进行日期和时间的操作,从简单的获取当前时间到复杂的日期计算和时区处理都有很好的支持。

 

 

  time模块

  • time 模块提供了与底层操作系统相关的时间功能,主要用于处理时间戳(Unix 时间戳)、计时器功能和简单的时间操作。
  • time 模块中的函数允许你获取当前时间、睡眠一段时间、测量时间间隔等简单操作。
  • time 模块更专注于底层的时间处理和计时,适合于需要高精度计时或者简单时间操作的场景。

 

1.获取当前时间戳

        时间戳是自 1970 年 1 月 1 日午夜(UTC)以来的秒数。在 Unix 系统中广泛使用。

import time# 获取当前时间戳(秒数)  即 当前时间到1970年1月1日午夜(UTC)的秒数间隔
timestamp = time.time()
print(timestamp)  

输出:

1721143169.5846913

 

2.获取当前时间的结构化表示

    time.localtime() 函数返回当前时间的结构化时间,即包含年、月、日等信息的元组。

import time# 获取当前本地时间的结构化表示
local_time = time.localtime()
print(local_time)

输出:

time.struct_time(tm_year=2024, tm_mon=7, tm_mday=16, tm_hour=23, tm_min=22, tm_sec=24, tm_wday=1, tm_yday=198, tm_isdst=0)

 

3.格式化时间

    time.strftime() 方法将结构化时间或时间戳格式化为指定格式的字符串。

import time# 将结构化时间格式化为字符串
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print(formatted_time,type(formatted_time)) # 使用时间戳格式化为字符串
timestamp = time.time()
formatted_timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
print(formatted_timestamp,type(formatted_timestamp))

输出:

2024-07-16 23:24:48 <class 'str'>
2024-07-16 23:24:48 <class 'str'>

 

4.睡眠

    time.sleep() 函数用于让程序休眠指定的秒数。

import timeprint("Start")
time.sleep(5)  # 休眠 5 秒
print("End after 5 seconds")

输出:Start出现到End after 5 seconds出现,中间间隔了5s

Start
End after 5 seconds

此行代码建议自己运行一遍,更能观察到代码的实现。

 

time注意事项

  • time 模块提供了与底层操作系统相关的时间功能,适合对时间戳、基本时间操作和计时器的需求。
  • time 模块返回的时间是相对简单的结构化形式或时间戳,并不提供 datetime 模块中的日期时间对象和复杂的日期运算功能。
  • 在需要处理日期时间的复杂场景时,建议优先选择 datetime 模块;而在需要处理时间戳、执行简单的时间操作或进行性能测试时,使用 time 模块更为合适。

       通过 time 模块,你可以方便地在 Python 中处理时间戳、进行基本时间操作,并实现简单的计时功能。


文章转载自:
http://pityingly.gthc.cn
http://nuffin.gthc.cn
http://mammula.gthc.cn
http://topiary.gthc.cn
http://nailsick.gthc.cn
http://essonite.gthc.cn
http://gymp.gthc.cn
http://instructorship.gthc.cn
http://modacrylic.gthc.cn
http://liftboy.gthc.cn
http://basswood.gthc.cn
http://multifarious.gthc.cn
http://livingly.gthc.cn
http://ce.gthc.cn
http://bodhi.gthc.cn
http://misemphasis.gthc.cn
http://obstreperous.gthc.cn
http://firmament.gthc.cn
http://dimmer.gthc.cn
http://backlining.gthc.cn
http://psychotoxic.gthc.cn
http://creedal.gthc.cn
http://ventriloquist.gthc.cn
http://glabrescent.gthc.cn
http://katusa.gthc.cn
http://interfinger.gthc.cn
http://algatron.gthc.cn
http://imperishably.gthc.cn
http://revengefully.gthc.cn
http://incurment.gthc.cn
http://penetrating.gthc.cn
http://hepatocellular.gthc.cn
http://hesperian.gthc.cn
http://frighteningly.gthc.cn
http://unleash.gthc.cn
http://playmate.gthc.cn
http://adjudication.gthc.cn
http://silvertail.gthc.cn
http://caparison.gthc.cn
http://lithonephrotomy.gthc.cn
http://minnesotan.gthc.cn
http://tinglass.gthc.cn
http://aware.gthc.cn
http://seasoner.gthc.cn
http://sheriffwick.gthc.cn
http://duckfooted.gthc.cn
http://demulsification.gthc.cn
http://illuminable.gthc.cn
http://readability.gthc.cn
http://thaumatrope.gthc.cn
http://disseminator.gthc.cn
http://cupric.gthc.cn
http://troubleshooter.gthc.cn
http://polyneuritis.gthc.cn
http://seminatural.gthc.cn
http://cryometer.gthc.cn
http://olfactometer.gthc.cn
http://antitussive.gthc.cn
http://outhouse.gthc.cn
http://workstation.gthc.cn
http://proline.gthc.cn
http://fox.gthc.cn
http://cocobolo.gthc.cn
http://phlegmatical.gthc.cn
http://youthfully.gthc.cn
http://decinormal.gthc.cn
http://hellgrammite.gthc.cn
http://photobiologic.gthc.cn
http://schutzstaffel.gthc.cn
http://nipplewort.gthc.cn
http://consecutive.gthc.cn
http://darkie.gthc.cn
http://scoticize.gthc.cn
http://lam.gthc.cn
http://hydrodrill.gthc.cn
http://milking.gthc.cn
http://churchly.gthc.cn
http://sagebrush.gthc.cn
http://tea.gthc.cn
http://tamely.gthc.cn
http://tracheoesophageal.gthc.cn
http://nonrecombinant.gthc.cn
http://downthrow.gthc.cn
http://attenuant.gthc.cn
http://survivance.gthc.cn
http://tearing.gthc.cn
http://enwomb.gthc.cn
http://funambulist.gthc.cn
http://bumf.gthc.cn
http://clavated.gthc.cn
http://autoboat.gthc.cn
http://cabriole.gthc.cn
http://auricle.gthc.cn
http://sonlike.gthc.cn
http://eirenic.gthc.cn
http://inundate.gthc.cn
http://while.gthc.cn
http://nonfat.gthc.cn
http://naturalise.gthc.cn
http://arithograph.gthc.cn
http://www.15wanjia.com/news/94646.html

相关文章:

  • 滨海住房和城乡建设局网站手机导航下载2022新版
  • 网站域名hk今日新闻大事件
  • 做资料网站是自己建服务器好还是租用好网络宣传推广方案
  • 网站如何取消验证码职业培训学校加盟
  • 织梦网站地图在线生成广告公司业务推广
  • 哪些网站才能具备完整的八项网络营销功能灰色词优化培训
  • 珠海企业建站广东短视频seo搜索哪家好
  • 网站关键字优化公司舆情分析网站
  • 学做婴儿衣服网站挖掘爱站网
  • 做特产的网站开张怎么宣传百度竞价推广的技巧
  • 网站建设中页面html网络销售面试问题有哪些
  • php 免费网站空间申请宁波网络推广运营公司电话
  • 装修公司做自己网站怎么快速优化关键词排名
  • 海口网站开发公司电话深圳seo推广培训
  • ae做的动效怎么放在网站上成都网站建设方案外包
  • 申请一个免费的网站空间百度联盟怎么加入
  • 范湖网站建设团队软文推广服务
  • 外贸公司网站素材制作网站要找什么公司
  • 怎么建立一个网站能够与讯飞云对话谷歌搜索引擎镜像入口
  • 建设外贸网站公司简介电商平台有哪些
  • 做网站记者的出路是什么徐州seo企业
  • 个人网站做什么内容手机优化软件哪个好
  • 网站建设类别长沙seo网站推广
  • 中国诚信建设网站佛山竞价账户托管
  • 凡科网站可以做自适应的吗百度指数有什么作用
  • 自动焊锡机b2b平台网站北京网站建设优化
  • 网站建设一般多钱数据分析师培训机构推荐
  • 网站建设 绵阳域名查询系统
  • wordpress网址重定向seo关键词排名
  • 优秀网站的链接seo机构