Appearance
实现 法定退休年龄计算器 工具
分类:developer | 标签:退休、延迟退休、政策 按 2025 延迟退休政策计算法定退休年龄
2.1 功能说明
按 2025 延迟退休政策计算法定退休年龄
2.1.1 使用指南
功能说明
依据 2024 年 9 月公布的《关于实施渐进式延迟法定退休年龄的决定》(2025 年 1 月 1 日起施行),按出生年月与人员类别计算法定退休年月、延迟月数及剩余时间,并给出弹性提前 / 延后区间。
使用场景
规划退休时间、核对社保缴费年限、估算养老金起领节点。
2.2 代码实现
2.2.1 组件结构
本工具是一个基于 Vue 3 <script setup> 语法的单文件组件(SFC),统一包裹在 ToolPageShell 组件内,由它提供页面标题、工具 ID、分类与「使用指南」插槽等通用外壳;核心业务逻辑(响应式状态、计算属性、事件处理函数)全部写在 <script setup> 中,输入/输出通过 el-input、el-button 等 Element Plus 组件与用户交互,所有数据均在浏览器本地处理,不会上传服务器。
2.2.2 核心逻辑概览
<script setup> 中定义的主要函数/方法:
addMonths()fmt()calc()
2.2.3 关键实现代码
以下是该工具的完整 <script setup> 实现(核心代码)。模板(<template>)与样式(<style>)已省略。
vue
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import ToolPageShell from '@/components/ToolPageShell.vue'
const birth = ref('')
const cat = ref('male')
const err = ref('')
const res = ref(null)
const RULES = {
male: { baseAge: 60, start: [1965, 1], step: 4, maxDelay: 36 },
female55: { baseAge: 55, start: [1970, 1], step: 4, maxDelay: 36 },
female50: { baseAge: 50, start: [1975, 1], step: 2, maxDelay: 60 }
}
function addMonths(y, m, n) {
let mm = m - 1 + n
return { y: y + Math.floor(mm / 12), m: (mm % 12) + 1 }
}
function fmt(y, m) { return `${y} 年 ${m} 月` }
function calc() {
err.value = ''; res.value = null
const m = (birth.value || '').match(/^(\d{4})-(\d{2})$/)
if (!m) { err.value = '请选择出生年月'; return }
const by = +m[1], bm = +m[2]
if (by < 1900 || by > 2010) { err.value = '出生年份超出合理范围(1900–2010)'; return }
const rule = RULES[cat.value]
const birthIdx = by * 12 + bm
const startIdx = rule.start[0] * 12 + rule.start[1]
let delay = 0
if (birthIdx >= startIdx) {
delay = Math.floor((birthIdx - startIdx) / rule.step)
if (delay > rule.maxDelay) delay = rule.maxDelay
}
const finalAge = rule.baseAge + delay / 12
const r = addMonths(by, bm, rule.baseAge * 12 + delay)
const retireYm = fmt(r.y, r.m)
const now = new Date()
const ny = now.getFullYear(), nm = now.getMonth() + 1
const nowIdx = ny * 12 + nm
const retireIdx = r.y * 12 + r.m
const diff = retireIdx - nowIdx
const passed = diff <= 0
const abs = Math.abs(diff)
const remainText = passed
? `已退休 ${abs} 个月(退休于 ${retireYm})`
: `约 ${Math.floor(abs / 12)} 年 ${abs % 12} 个月(${abs} 个月)`
const e = addMonths(r.y, r.m, -36)
const l = addMonths(r.y, r.m, 36)
const age = ny - by - (nm < bm ? 1 : 0)
res.value = {
age, baseAge: rule.baseAge, delay, finalAge: finalAge % 1 === 0 ? finalAge : finalAge.toFixed(1),
retireYm, passed, remainText,
earlyYm: fmt(e.y, e.m), lateYm: fmt(l.y, l.m)
}
ElMessage.success('计算完成')
}2.3 效果截图
