首页 > 编程知识 正文

日期计算python(日期计算天数在线计算)

时间:2023-12-17 21:42:39 阅读:316691 作者:SUJW

本文目录一览:

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中的日期推算

#coding=utf-8

'''

Created on 2014-12-29

@author: NeoWu

'''

c=0

import calendar

for year in xrange(1901,1902):

    for month in xrange(1,13):

        '''

        1.加了些打印帮助你理解

        2.calendar.monthcalendar(year,month)这个返回的是传入的那一年的某个月的【星期列表】:

        [[0, 0, 0, 0, 0, 0, 1], 

        [2, 3, 4, 5, 6, 7, 8], 

        [9, 10, 11, 12, 13, 14, 15], 

        [16, 17, 18, 19, 20, 21, 22], 

        [23, 24, 25, 26, 27, 28, 29], 

        [30, 31, 0, 0, 0, 0, 0]]

        3.calendar.monthcalendar(year,month)[0]取的列表中的第一个元素:

        [0, 0, 0, 0, 0, 0, 1]

        4.calendar.monthcalendar(year,month)[0].index(1)返回1出现的位置

                    代码中判断该值为6,意思是,这个月的1号是星期6

        '''

        if calendar.monthcalendar(year,month)[0].index(1) == 6:

            #--------------------------------------------------

            print 'Date: %d-%d(year-month)' % (year,month)

            print 'SuntMontTuetWedtThutFritSat'

            for e in calendar.monthcalendar(year,month):

                print '%dt%dt%dt%dt%dt%dt%d' % (e[0],e[1],e[2],e[3],e[4],e[5],e[6])

            #--------------------------------------------------

            print calendar.monthcalendar(year,month)

            print calendar.monthcalendar(year,month)[0]

            print calendar.monthcalendar(year,month)[0].index(1)

            c += 1

    print c

结果:

Date: 1901-9(year-month)

Sun Mon Tue Wed Thu Fri Sat

0 0 0 0 0 0 1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 0 0 0 0 0 0

[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 0, 0, 0, 0, 0, 0]]

[0, 0, 0, 0, 0, 0, 1]

6

Date: 1901-12(year-month)

Sun Mon Tue Wed Thu Fri Sat

0 0 0 0 0 0 1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31 0 0 0 0 0

[[0, 0, 0, 0, 0, 0, 1], [2, 3, 4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15], [16, 17, 18, 19, 20, 21, 22], [23, 24, 25, 26, 27, 28, 29], [30, 31, 0, 0, 0, 0, 0]]

[0, 0, 0, 0, 0, 0, 1]

6

2

python给出年/月/日计算是此年的多少天?

import datetime

import calendar

year = int(input('请输度入4位数字的年份:'))  # 获取年份

month= int(input('请输入月份1到12之间:'))  # 获取月份

day= int(input('请输入日份1到31之间:'))  # 获取“日”

if(calendar.isleap(year)==True):

print('闰年')

else:

print('平年')

if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):

print('31天')

elif (month == 4 or month == 6 or month == 9 or month == 11 ):

print('30天')

elif month == 2 and ((year % 4==0 and year % 100!=0) or (year % 400==0)):

print('29天')

else:

print('28天')

targetDay = datetime.date(year, month, day)  # 将输入的日期专格式化成标准的日期

dayCount = targetDay - datetime.date(targetDay.year - 1, 12, 31)  # 减去上一属年最后一天

print('%s是%s年的第%s天。' % (targetDay, year, dayCount.days))

python 求日期

# -*- coding: cp936 -*-

#设置星期天的初始值为0

mondays=0

def getmonthdays(year):

    isleapyear=year%400==0 or (year%4==0 and (not year%100==0))

    if isleapyear:

        return [31,29,31,30,31,30,31,31,30,31,30,31]

    return [31,28,31,30,31,30,31,31,30,31,30,31]

#计算1899.12.31(这天是星期天)1901.1.1之间的天数

pastdays=1  #1899.12.31过一天是1900.1.1

monthdays=getmonthdays(1900)

for month in range (0,12):

    pastdays+=monthdays[month]

#计算1901.1.1到2000.12.31星期天的数字

for year in range(1901,2001):

    monthdays=getmonthdays(year)

    for month in range(0,12):

        if pastdays%7==0:

            mondays+=1

        pastdays+=monthdays[month]

print "1901年1月1月至2000年12月31日共有%d个星期天落在每月第一天"%mondays

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。