Jquery datepicker 时间插件使用 js 时间相加,相减
$(document).ready(function(){ //输入框事件 $('#probation').bind('input propertychange', function() { var induction = $("#induction").val();//取出时间 if (""==induction||null==induction||undefined==induction){ return alert("请先输入入职日期"); } var addDate =new Date(induction); var probation = $("#probation").val();//取出输入值 var newDate = DateAdd(probation,addDate); //初始化值 $('#inDueForm').val(date_riqi(newDate,0)) }); //日期选择事件(此处为转正日期) $("#inDueForm").on("hide.uk.datepicker", function (event) { var init = $(this).val(); var induction = $("#induction").val();//取出入职时间 if (""==induction||null==induction||undefined==induction){ return alert("请先输入入职日期"); } var probation= DateDiff(init,induction); $("#probation").val(probation);//设置试用期输入值 }); });
<div class="uk-form-row"> <label class="uk-form-label" for="induction">入职时间(必填)</label> <div class="uk-form-controls"> <input name="induction" data-uk-datepicker="{format:'YYYY-MM-DD'}" id="induction" value="" > </div> </div> <div class="uk-form-row"> <label class="uk-form-label" for="probation">试用期</label> <div class="uk-form-controls"> <input id="probation" name="probation" value="" placeholder="" type="number"> </div> </div> <div class="uk-form-row"> <label class="uk-form-label" for="inDueForm">转正时间</label> <div class="uk-form-controls"> <input id="inDueForm" data-uk-datepicker="{format:'YYYY-MM-DD'}" name="inDueForm" value=""> </div> </div>
上面js用到的方法如下:可以放到utils里面
微信公众号:

//日期格式 function date_riqi(date,flag){ /** *flag =0 返回 yyyy-mm-dd *flag =1 返回 yyyy-mm-dd hh:mm:ss *flag =2 返回 yyyy-mm-dd hh:mm:ss.ms */ if(typeof(date) == "undefined"){ return ""; } var date = new Date(date); var str_date=""; var month=(date.getMonth()+1); month=month<10?"0"+month:month; var day = date.getDate(); day = day<10?"0"+day:day; var hour = date.getHours(); hour = hour<10?"0"+hour:hour; var minutes = date.getMinutes(); minutes = minutes<10?"0"+minutes:minutes; var seconds = date.getSeconds(); seconds = seconds<10?"0"+seconds:seconds; var ms = date.getMilliseconds(); ms = ms<10?"00"+ms:ms<100?"0"+ms:ms; if (flag==0){ str_date=date.getFullYear()+"-"+month+"-"+day; } if (flag==1){ str_date=date.getFullYear()+"-"+month+"-"+day+ " "+hour+":"+minutes+":"+seconds; } if (flag==2){date.getMinutes() str_date=date.getFullYear()+"-"+month+"-"+day+ " "+hour+":"+minutes+":"+seconds+"."+ms; } return str_date; } //时间加天 function DateAdd(number, date) { date.setDate(date.getDate() + Number(number)); return date; } //返回相差天数 var DateDiff=function (sDate1, sDate2) { //sDate1和sDate2是yyyy-MM-dd格式 var aDate, oDate1, oDate2, iDays; aDate = sDate1.split("-"); oDate1 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]); //转换为yyyy-MM-dd格式 aDate = sDate2.split("-"); oDate2 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]); iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数 return iDays; //返回相差天数 };
当然还要加载插件:(html head)

<head> <title></title> <meta charset="UTF-8"> <script src="https://cdn.bootcss.com/jquery/2.1.3/jquery.js"></script> <script src="https://cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script> <script src="https://cdn.bootcss.com/uikit/2.27.2/js/uikit.min.js"></script> <link href="https://cdn.bootcss.com/uikit/2.27.2/css/components/datepicker.css" rel="stylesheet"> <script src="https://cdn.bootcss.com/uikit/2.26.4/js/components/datepicker.js"></script> </head>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了