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

  • 1、python日期获取秒数
  • 2、关于python中的日期推算
  • 3、python给出年/月/日计算是此年的多少天?
  • 4、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 日期天数差

#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 ‘Sun\tMon\tTue\tWed\tThu\tFri\tSat’

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

                print ‘%d\t%d\t%d\t%d\t%d\t%d\t%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

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))

# -*- 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

原创文章,作者:LMFRP,如若转载,请注明出处:https://www.506064.com/n/126227.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
LMFRP的头像LMFRP
上一篇 2024-10-03 23:07
下一篇 2024-10-03 23:07

相关推荐

  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29

发表回复

登录后才能评论