使用python计算一年有多少秒

导读:很多朋友问到关于使用python计算一年有多少秒的相关问题,本文首席CTO笔记就来为大家做个详细解答,供大家参考,希望对大家有所帮助!一起来看看吧!

用Python,从键盘任意输入一个年,计算这个年是多少天。比如:输入2019年,要首先判断是否闰年

def leap_year_or_not(year):

    # 世纪闰年:能被400整除的为世纪闰年。

    # 普通闰年:能被4整除但不能被100整除的年份为普通闰年。

    # 闰年共有366天,其他年只有365天。

    

    if int(year) % 400 == 0:

        return True

    elif int(year) % 100 !=0 and int(year) % 4 == 0:

        return True

    else:

        return False

def calculate_days_of_year(year):

    leap = leap_year_or_not(year)

    if leap:

        days = 366

        run = "是"

    else:

        days = 365

        run = "不是"

    print("{}年{}闰年,有{}天。".format(year, run, days))

if __name__ == "__main__":

    print("输入年份:")

    n = input()

    calculate_days_of_year(n)

运行上述代码,输入2019回车,得到以下结果:

Python 计算一年有多少秒

#coding=utf-8

import calendar

def getsec(year):

    all_days=0

    for i in range(1,13):

        all_days = calendar.monthrange(year,i)[1]+all_days

    return all_days*24*60*60*60

print getsec(2017)

使用python计算一年有多少秒  第1张

一年有多少秒?怎么算出来?

1、按365天算。一天为24小时,一小时有60分钟,3600秒,按365天算,也就是平年,有365*24*60*60=31536000秒。

2、按366天算。也就是按照闰年算,有366*24*60*60=31622400秒。

3、按恒星年(公转360度)算。这就是按照365天6时9分10秒算,有31558150秒。

4、按回归年算。回归年是365天5时48分46秒,总计有31556926秒。

Python简单计算一年有多少小时,分钟,秒

#coding=utf-8

import calendar

def getsec(year):

    all_days=0

    for i in range(1,13):

        all_days = calendar.monthrange(year,i)[1]+all_days

    return all_days*24*60*60*60

print getsec(2017)

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); //后一天

以下图片使用后台输出表示。

扩展资料

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 日期天数差

用python计算时间长

方法1:

import datetime

starttime = datetime.datetime.now()

#long running

#do something other

endtime = datetime.datetime.now()

print (endtime - starttime).seconds

datetime.datetime.now()获取的是当前日期,在程序执行结束之后,这个方式获得的时间值为程序执行的时间。

方法2:

start = time.time()

#long running

#do something other

end = time.time()

print end-start

time.time()获取自纪元以来的当前时间(以秒为单位)。如果系统时钟提供它们,则可能存在秒的分数。所以这个地方返回的是一个浮点型类型。这里获取的也是程序的执行时间。

结语:以上就是首席CTO笔记为大家整理的关于使用python计算一年有多少秒的相关内容解答汇总了,希望对您有所帮助!如果解决了您的问题欢迎分享给更多关注此问题的朋友喔~

以上内容为新媒号(sinv.com.cn)为大家提供!新媒号,坚持更新大家所需的互联网后端知识。希望您喜欢!

版权申明:新媒号所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流,不声明或保证其内容的正确性,如发现本站有涉嫌抄袭侵权/违法违规的内容。请发送邮件至 k2#88.com(替换@) 举报,一经查实,本站将立刻删除。

(0)
上一篇 2023-09-23 13:18
下一篇 2023-09-23 13:18

相关推荐

发表回复

登录后才能评论