Python获取当前时间

time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))  # 当前时间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的datetime模块来获取当前时间。具体实现代码如下: ```python import datetime now = datetime.datetime.now() print("当前时间为:", now) ``` 输出结果如下所示: ``` 当前时间为: 2021-06-03 16:31:42.465262 ``` ### 回答2: Python获取当前时间可以使用datetime模块。首先需要导入datetime模块,然后使用datetime模块下的datetime类来获取当前时间。 具体的步骤如下: 1. 导入datetime模块:`import datetime` 2. 使用datetime类获取当前时间:`current_time = datetime.datetime.now()` 3. 可以通过current_time的属性获取当前时间的年、月、日、时、分、秒等信息。 以下是一个例子: ```python import datetime current_time = datetime.datetime.now() # 获取当前时间的年、月、日、时、分、秒 year = current_time.year month = current_time.month day = current_time.day hour = current_time.hour minute = current_time.minute second = current_time.second print("当前时间:", current_time) print("年:", year) print("月:", month) print("日:", day) print("时:", hour) print("分:", minute) print("秒:", second) ``` 运行上述代码,输出结果如下: ``` 当前时间: 2023-04-19 09:25:34.123456 年: 2023 月: 4 日: 19 时: 9 分: 25 秒: 34 ``` 以上就是使用Python获取当前时间的方法。 ### 回答3: 在Python中,可以使用datetime模块获取当前时间。 首先,我们需要导入datetime模块,然后使用datetime模块的datetime类来获取当前时间。代码如下: ``` import datetime current_time = datetime.datetime.now() print("当前时间为:", current_time) ``` 这段代码首先导入了datetime模块,然后使用datetime类的now()方法获取当前时间,并将结果赋值给current_time变量。 最后,我们使用print()函数将当前时间打印出来,输出的结果形如:"当前时间为:2022-02-22 13:45:30.123456"。 需要注意的是,获取当前时间是计算机系统的当前时间,而不是用户所在地的当前时间。如果需要获取用户所在地的当前时间,可以考虑使用第三方库pytz来进行时区的转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值