A. python 從鍵盤上輸入小時、分鍾、秒3個數,計算總時間為多少秒
h,m,s=map(int,input().split())
print(h*3600+m*60+s)
B. 新手求教:python 時間格式轉換
時間格式轉換分為兩種,時間轉換為字元串和字元串轉換為時間,具體代碼例子如下:
1importdatetime
2importtime
3#日期轉換為字元串,使用strftime()函數
4#time.strftime(format[,t])
5
6printdatetime.datetime.now()
7printdatetime.datetime.now().strftime("%Y-%m-%d
%H:%M:%S")
8printdatetime.datetime.now().strftime("%b
%d%Y%H:%M:%S")
9printdatetime.datetime.now().strftime("%c
%d%Y%H:%M:%S")
10#字元串轉換為日期,使用strptime()函數
11t=(2009,2,17,8,3,38,1,48,0)
12t=time.mktime(t)
13printtime.strftime("%b%d%Y%H:%M:%S",time.gmtime(t))
14printtime.strftime("%Y-%m-%d%H:%M:%S",time.gmtime(t))
註:格式字元說明:
python中時間日期格式化符號:
%y
兩位數的年份表示(00-99)
%Y
四位數的年份表示(000-9999)
%m
月份(01-12)
%d
月內中的一天(0-31)
%H
24小時制小時數(0-23)
%I
12小時制小時數(01-12)
%M
分鍾數(00=59)
%S
秒(00-59)
%a
本地簡化星期名稱
%A
本地完整星期名稱
%b
本地簡化的月份名稱
%B
本地完整的月份名稱
%c
本地相應的日期表示和時間表示
%j
年內的一天(001-366)
%p
本地A.M.或P.M.的等價符
%U
一年中的星期數(00-53)星期天為星期的開始
%w
星期(0-6),星期天為星期的開始
%W
一年中的星期數(00-53)星期一為星期的開始
%x
本地相應的日期表示
%X
本地相應的時間表示
%Z
當前時區的名稱
%%
%號本身
C. python 24小時制時間轉秒時間轉秒
>>> import datetime
>>>
>>> now = datetime.datetime.now()
>>> nowtime = now.time()
>>> nowtime.hour * 3600 + nowtime.minute * 60 + nowtime.second
70320
D. python日期獲取秒數
1、使用new Date()獲取當前日期,new Date().getTime()獲取當前毫秒數
2、計算公式,等於獲取的當前日期減去或者加上一天的毫秒數。一天的毫秒數的計算公式:24小時*60分鍾*60秒*1000毫秒,也是86400000毫秒。
舉例:
Date curDate = new Date();
var preDate = new Date(curDate.getTime() - 24*60*60*1000); //前一天
var nextDate = new Date(curDate.getTime() + 24*60*60*1000); //後一天
以下圖片使用後台輸出表示。
(4)根據秒數換算時間python擴展閱讀
var myDate = new Date();
myDate.getYear(); //獲取當前年份(2位)
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
myDate.getMonth(); //獲取當前月份(0-11,0代表1月)
myDate.getDate(); //獲取當前日(1-31)
myDate.getDay(); //獲取當前星期X(0-6,0代表星期天)
myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //獲取當前小時數(0-23)
myDate.getMinutes(); //獲取當前分鍾數(0-59)
myDate.getSeconds(); //獲取當前秒數(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(0-999)
myDate.toLocaleDateString(); //獲取當前日期
var mytime=myDate.toLocaleTimeString(); //獲取當前時間
myDate.toLocaleString( ); //獲取日期與時間
Date.prototype.isLeapYear 判斷閏年
Date.prototype.Format 日期格式化
Date.prototype.DateAdd 日期計算
Date.prototype.DateDiff 比較日期差
Date.prototype.toString 日期轉字元串
Date.prototype.toArray 日期分割為數組
Date.prototype.DatePart 取日期的部分信息
Date.prototype.MaxDayOfDate 取日期所在月的最大天數
Date.prototype.WeekNumOfYear 判斷日期所在年的第幾周
StringToDate 字元串轉日期型
IsValidDate 驗證日期有效性
CheckDateTime 完整日期時間檢查
daysBetween 日期天數差
E. python 將這個時間格式轉化為秒數
先把時間格式「1:23:20:27.00」 轉化為字元串
s1="1:23:20:27.00"
#a=time.strptime(s1,'%d:%H:%m:%S')
print s1.replace(':','').replace('.','')
#print s1.split(':')
do = {0:24*60*60,1:60*60,2:60,3:1}
ls2=[i for i in s1.split(':')]
print sum([ int(float(i)) * do[ls2.index(i)] for i in ls2])
>>> ================================ RESTART ================================
>>>
123202700
170427
>>>
F. python 計算時間差秒
python 計算時間差秒:
上例演示了計算當前時間向後10小時的時間。
G. python time.ctime怎麼計算
描述
Python time ctime() 函數把一個時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。 如果參數未給或者為None的時候,將會默認time.time()為參數。它的作用相當於 asctime(localtime(secs))。
語法
ctime()方法語法:
time.ctime([ sec ])
參數
sec -- 要轉換為字元串時間的秒數。
返回值
該函數沒有任何返回值。
實例
以下實例展示了 ctime() 函數的使用方法:
#!/usr/bin/python
import time
print "time.ctime() : %s" % time.ctime()
以上實例輸出結果為:
time.ctime() : Tue Feb 17 10:00:18 2013
H. 如何用python獲得當前時間的秒數
importdatetime
now=datetime.datetime.now()
printnow.second
now裡面有其所有日期和時間的屬性
now.second獲得的是int型
I. python 語言中,現在有字元串「11:45」,想計算現在時間同「11:45」 之間的秒數,應該怎麼計算呢
def ISOString2Time( s ):#將標准時間轉成秒
return time.mktime( time.strptime( s, '%Y-%m-%d %X' ) )
def Time2ISOString( s ):#將秒轉成標准時間
return time.strftime( '%Y-%m-%d %X', time.localtime( float( s ) ) )
用上面的函數,將你需要的2個時間,轉成秒,然後相減得到結果