時間和日期

2021-08-10 14:34:52 字數 1066 閱讀 8753

獲取當前時間戳:

import time

sticks=time.time()

print(sticks)

import time

localtime=time.localtime(time.time())

print(localtime)

用asctime

import time

localtime=time.asctime(time.localtime(time.time()))

格式化日期:

#!/usr/bin/python

# -*- coding: utf-8 -*-

import

time

# 格式化成2016-03-20 11:45:39形式

print

time

.strftime

("%y-%m-%d %h:%m:%s"

,time

.localtime

())# 格式化成sat mar 28 22:24:24 2016形式

print

time

.strftime

("%a %b %d %h:%m:%s %y"

,time

.localtime

())# 將格式字串轉換為時間戳a =

"sat mar 28 22:24:24 2016"

print

time

.mktime

(time

.strptime(a

,"%a %b %d %h:%m:%s %y"

))

以上例項輸出結果:

2016-04

-0710:

25:09thu

apr0710:

25:092016

1459175064.0

獲取某月日曆:

import calendar

cal=time.month(2017,11)

print(cal)

時間和日期

日期時間 1.系統時間的獲取 from datetime import datetime dt datetime.now print type dt print dt print dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second,dt.month...

日期和時間

預設直接輸入 date 顯示當前系統時間 高階使用方法 1.date option format 2.date u utc universal mmddhhmm cc yy ss 第一種用法是一種顯示時間方法 1 root centos7 date y m d 2 20170519 3 root c...

日期和時間

var then new date 2010,0,1 2010年1月1日 var later new date 2010,0,1,17,10,30 2010年1月1日17 10 30 var now new date 獲取當前時間 var elapsed now then 日期減 計算時間間隔的毫秒...