- 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