日期加减计算
对日期进行加减运算
时间差计算
计算两个日期之间的时间差
工作日计算
计算工作日(排除周末和节假日)
年龄计算
根据出生日期计算年龄
生肖星座查询
查询指定日期的生肖和星座
代码示例
以下是在不同编程语言中进行日期计算的代码示例:
JavaScript 日期计算
// 日期加减
function addDate(date, days, months, years) {
const result = new Date(date);
if (years) result.setFullYear(result.getFullYear() + years);
if (months) result.setMonth(result.getMonth() + months);
if (days) result.setDate(result.getDate() + days);
return result;
}
// 计算两个日期的差值(天数)
function dateDiffInDays(date1, date2) {
const oneDay = 24 * 60 * 60 * 1000;
return Math.abs(Math.round((date2 - date1) / oneDay));
}
// 计算年龄
function calculateAge(birthDate) {
const today = new Date();
const birth = new Date(birthDate);
let years = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
years--;
}
return years;
}
// 获取星座
function getConstellation(date) {
const month = date.getMonth() + 1;
const day = date.getDate();
const constellations = [
{ name: '摩羯座', start: [1, 1], end: [1, 19] },
{ name: '水瓶座', start: [1, 20], end: [2, 18] },
{ name: '双鱼座', start: [2, 19], end: [3, 20] },
{ name: '白羊座', start: [3, 21], end: [4, 19] },
{ name: '金牛座', start: [4, 20], end: [5, 20] },
{ name: '双子座', start: [5, 21], end: [6, 21] },
{ name: '巨蟹座', start: [6, 22], end: [7, 22] },
{ name: '狮子座', start: [7, 23], end: [8, 22] },
{ name: '处女座', start: [8, 23], end: [9, 22] },
{ name: '天秤座', start: [9, 23], end: [10, 23] },
{ name: '天蝎座', start: [10, 24], end: [11, 22] },
{ name: '射手座', start: [11, 23], end: [12, 21] },
{ name: '摩羯座', start: [12, 22], end: [12, 31] }
];
for (const c of constellations) {
if ((month === c.start[0] && day >= c.start[1]) ||
(month === c.end[0] && day <= c.end[1])) {
return c.name;
}
}
}
// 获取生肖
function getChineseZodiac(year) {
const zodiacs = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪'];
return zodiacs[(year - 4) % 12];
}
Python 日期计算
from datetime import datetime, timedelta
from dateutil import relativedelta
# 日期加减
def add_date(date, days=0, weeks=0, months=0, years=0):
result = date + timedelta(days=days, weeks=weeks)
if months or years:
result += relativedelta.relativedelta(months=months, years=years)
return result
# 计算两个日期的差值
def date_diff(date1, date2):
diff = abs(date2 - date1)
return {
'days': diff.days,
'seconds': diff.seconds,
'total_seconds': diff.total_seconds()
}
# 计算年龄
def calculate_age(birth_date):
today = datetime.now()
age = today.year - birth_date.year - (
(today.month, today.day) < (birth_date.month, birth_date.day)
)
return age
# 获取星座
def get_constellation(date):
month = date.month
day = date.day
constellations = [
('摩羯座', [(1, 1), (1, 19)]),
('水瓶座', [(1, 20), (2, 18)]),
('双鱼座', [(2, 19), (3, 20)]),
('白羊座', [(3, 21), (4, 19)]),
('金牛座', [(4, 20), (5, 20)]),
('双子座', [(5, 21), (6, 21)]),
('巨蟹座', [(6, 22), (7, 22)]),
('狮子座', [(7, 23), (8, 22)]),
('处女座', [(8, 23), (9, 22)]),
('天秤座', [(9, 23), (10, 23)]),
('天蝎座', [(10, 24), (11, 22)]),
('射手座', [(11, 23), (12, 21)]),
('摩羯座', [(12, 22), (12, 31)])
]
for name, ((start_m, start_d), (end_m, end_d)) in constellations:
if (month == start_m and day >= start_d) or (month == end_m and day <= end_d):
return name
# 获取生肖
def get_chinese_zodiac(year):
zodiacs = ['鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪']
return zodiacs[(year - 4) % 12]
Java 日期计算
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
public class DateCalculator {
// 日期加减
public static LocalDate addDate(LocalDate date, long days, long months, long years) {
LocalDate result = date;
if (days > 0) result = result.plusDays(days);
if (months > 0) result = result.plusMonths(months);
if (years > 0) result = result.plusYears(years);
return result;
}
// 计算日期差值
public static long dateDiffInDays(LocalDate date1, LocalDate date2) {
return Math.abs(ChronoUnit.DAYS.between(date1, date2));
}
// 计算年龄
public static int calculateAge(LocalDate birthDate) {
return Period.between(birthDate, LocalDate.now()).getYears();
}
// 获取星座
public static String getConstellation(LocalDate date) {
int month = date.getMonthValue();
int day = date.getDayOfMonth();
if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) return "白羊座";
if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) return "金牛座";
if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) return "双子座";
if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) return "巨蟹座";
if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) return "狮子座";
if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) return "处女座";
if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) return "天秤座";
if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) return "天蝎座";
if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) return "射手座";
if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) return "摩羯座";
if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) return "水瓶座";
if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) return "双鱼座";
return "未知";
}
// 获取生肖
public static String getChineseZodiac(int year) {
String[] zodiacs = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
return zodiacs[(year - 4) % 12];
}
}
关于日期计算
常见日期计算场景
- 项目管理:计算项目开始和结束日期之间的工作日
- 合同管理:计算合同到期日期
- 财务计算:计算贷款到期日、利息计算周期
- 人事管理:计算员工工龄、年假天数
- 活动策划:计算活动筹备时间
星座日期范围
- 白羊座:3月21日 - 4月19日
- 金牛座:4月20日 - 5月20日
- 双子座:5月21日 - 6月21日
- 巨蟹座:6月22日 - 7月22日
- 狮子座:7月23日 - 8月22日
- 处女座:8月23日 - 9月22日
- 天秤座:9月23日 - 10月23日
- 天蝎座:10月24日 - 11月22日
- 射手座:11月23日 - 12月21日
- 摩羯座:12月22日 - 1月19日
- 水瓶座:1月20日 - 2月18日
- 双鱼座:2月19日 - 3月20日
工作日计算说明
工作日计算通常排除周六和周日。不同国家的法定节假日不同,需要根据实际情况配置节假日列表。