网站关键词重要性,手表网站查询,设计个企业网站网页咋弄,泊头 网站优化1、为什么做这个#xff1f; 工作中#xff0c;经常需要计算参保人社保、医保缴费月数#xff0c;之前都是在Excel中写一个DATEDIF公式#xff0c;修改单元格中的日期#xff0c;计算间隔的月数#xff0c;公式如下#xff1a; DATEDIF(起始日期, 终止日期, 返回类型) …1、为什么做这个 工作中经常需要计算参保人社保、医保缴费月数之前都是在Excel中写一个DATEDIF公式修改单元格中的日期计算间隔的月数公式如下 DATEDIF(起始日期, 终止日期, 返回类型) 或者 看起来这样做也挺方便但每次录入前需要用鼠标点击相应单元格清除内容录入……计算得多了仍然感觉很麻烦于是决心做一个“计算器”。
2、制作思路 这个计算器主打一个“方便”所以界面要简洁功能要强大功能主要做2个
根据2个日期计算相距的月数根据1个日期计算其之前或之后n个月的日期 在这里我设置了3种计算方式共计、相差、间隔在工作中“共计”模式用的最多。
共计从年月a数到年月b一共有多少个月例如从2008年1月至2008年9月计算结果为9相差从年月a到年月b差着几个月例如从2008年1月至2008年9月计算结果为8间隔从年月a到年月b中间隔着几个月例如从2008年1月至2008年9月计算结果为7。 为方便使用年月a和年月b自动比较大小录入时无需考虑哪个在前哪个在后。
3、绘制界面 界面分为上下两部分上面是按2个【年月】计算【月数】下面是按【年月一】【月数】计算【年月二】。 计算方式那里用了ComboBox下拉选择年月输入框使用的TextBox后续计算时要对有效性进行校验。 为方便操作对控件的OnKeyPress事件进行了设置实现用回车键切换文本框对窗体的CancelButton进行了设置实现用ESC键清空内容。
4、功能实现
直接粘上用到的2个方法 /// summary/// 按【年月一】【月数】计算【年月二】/// /summary/// param nameym1【年月一】/param/// param namemonthsCount【月数】/param/// param namejisuanfangshi计算方式/param/// param nameMsg存放错误信息/param/// returns【年月二】/returnsprivate DateTime calMonth(DateTime ym1, int monthsCount, string jisuanfangshi, out string Msg){DateTime dtResult new DateTime();try{dtResult ym1.AddMonths(monthsCount);Msg ;}catch(Exception ex){Msg ex.Message;}return dtResult;}/// summary/// 按2个【年月】计算【月数】/// /summary/// param namestart【年月一】/param/// param nameend【年月二】/param/// param namejisuanfangshi计算方式/param/// param nameMsg存放说明信息/param/// returns【月数】/returnsprivate int cal(DateTime start, DateTime end, string jisuanfangshi, out string Msg){int result -1;Msg ;if (start end){DateTime tmpDT end;end start;start tmpDT;}int endMonth end.Month;int startMonth start.Month;int endYear end.Year;int startYear start.Year;if (endMonth startMonth){endYear - 1;endMonth 12;}result 12 * (endYear - startYear) endMonth - startMonth;switch (jisuanfangshi){case 共计:result 1;break;case 间隔:result - 1;if (result 0){result 0;}break;case 相差:break;}if (result 12){Msg (result / 12).ToString() 年 (result % 12).ToString() 个月;}return result;}
按日期计算月数 private void btnCal_Click(object sender, EventArgs e){if (!Regex.IsMatch(txtStart.Text, ^\d{6}$)){MessageBox.Show(起始年月格式{yyyyMM}录入有误请检查, 消息, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);txtStart.Focus();return;}if (!Regex.IsMatch(txtEnd.Text, ^\d{6}$)){MessageBox.Show(截止年月格式{yyyyMM}录入有误请检查, 消息, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);txtEnd.Focus();return;}try{DateTime dt1 new DateTime(int.Parse(txtStart.Text.Substring(0, 4)), int.Parse(txtStart.Text.Substring(4, 2)), 1);DateTime dt2 new DateTime(int.Parse(txtEnd.Text.Substring(0, 4)), int.Parse(txtEnd.Text.Substring(4, 2)), 1);string info ;txtResult.Text cal(dt1, dt2, cmbCal.Text,out info).ToString();if (info.Length 0){lblInfo.Text 个月 即 info;}else{lblInfo.Text 个月;}}catch{MessageBox.Show(日期转换出错请检查, 消息, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);return;}}
按一个日期月数计算另一个日期 private void btnCalMonth_Click(object sender, EventArgs e){DateTime dtFirst;try{dtFirst new DateTime(int.Parse(txtYMA.Text.Substring(0, 4)),int.Parse(txtYMA.Text.Substring(4, 2)),1);}catch{MessageBox.Show(【起始年月】填写有误, 消息, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);txtYMA.Focus();return;}if (txtNumYear.Text.Length 0 txtNumMonth.Text.Length 0){MessageBox.Show(cmbCountMonth.Text【月数】未填写, 消息, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);txtNumYear.Focus();return;}int monthCount 1;if (cmbCalMonth.Text 间隔){monthCount -1;}if (cmbCalMonth.Text 相差){monthCount 0;}if(Regex.IsMatch(txtNumYear.Text,^\d$)){monthCount-int.Parse(txtNumYear.Text)*12;}if(Regex.IsMatch(txtNumMonth.Text,^\d$)){monthCount-int.Parse(txtNumMonth.Text);}if (cmbCountMonth.Text 后){monthCount * -1;}DateTime dtResult dtFirst.AddMonths(monthCount);txtResultCount.Text dtResult.ToString(yyyyMM);}
感兴趣的朋友可以动手做一个试一下下载源码https://download.csdn.net/download/W2KExp/88094039