日期計算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/zh-tw/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

發表回復

登錄後才能評論