Python使用Datetime模块

Datetime模块

  • 当前时间
  • 计算时间差

当前时间

now = datetime.now()
    print(datetime.today())
    print(now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond)
    print(now.strftime('%Y年%m月%d日%H时%M分%S秒%f毫秒'))
    print(now.date(), '--', now.ctime())

获取当前时间并打印,结果如下图:

Python使用Datetime模块_第1张图片

计算时间差

a = datetime.now()
    time.sleep(10)
    b = datetime.now()
    c = b - a
    print(c.total_seconds())
    print('time cost:{}s {}ms'.format(c.seconds, c.microseconds))

结果如下
时间差

你可能感兴趣的:(Python,python,开发语言)