當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
javascript对时间的校验操作
生活随笔
收集整理的這篇文章主要介紹了
javascript对时间的校验操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
function check()
{var startTime=$("#sTime").val();var endTime=$("#eTime").val();if(startTime==""){alert("請輸入開始時間!");return;}if(endTime==""){alert("請輸入結束時間!");return;}if(!checkEndTime(startTime,endTime)){alert("結束時間必須晚于開始時間!");return;}var sp=DateDiff(startTime,endTime);if(sp.Days>90){alert("只能查詢三個月內的數據!")return;}}function checkEndTime(startTime,endTime){var start=new Date(startTime.replace("-", "/").replace("-", "/"));var end=new Date(endTime.replace("-", "/").replace("-", "/"));if(end<start){return false;}return true;}function?DateDiff(sDate1,?sDate2){?//sDate1和sDate2是字符串?yyyy-MM-dd格式?var?aDate,?oDate1,?oDate2,?iDays,?ihours,?iminutes,?iseconds;aDate?=?sDate1.split("-");oDate1?=?new?Date(aDate[1]?+?'-'?+?aDate[2]?+?'-'?+?aDate[0]);//轉換為MM-dd-yyyy格式?aDate?=?sDate2.split("-");oDate2?=?new?Date(aDate[1]?+?'-'?+?aDate[2]?+?'-'?+?aDate[0]);var?timeSpan?=?{};var?TotalMilliseconds?=?Math.abs(oDate1?-?oDate2);//相差的毫秒數timeSpan.Days?=?parseInt(TotalMilliseconds?/?1000?/?60?/?60?/24);timeSpan.TotalHours?=?parseInt(TotalMilliseconds?/?1000?/?60?/?60);timeSpan.Hours?=?timeSpan.TotalHours?%?24;timeSpan.TotalMinutes?=?parseInt(TotalMilliseconds?/?1000?/?60);timeSpan.Minutes?=?timeSpan.TotalMinutes?%?60;timeSpan.TotalSeconds?=?parseInt(TotalMilliseconds?/?1000);timeSpan.Seconds?=?timeSpan.TotalSeconds?%?60;timeSpan.TotalMilliseconds?=?TotalMilliseconds;timeSpan.Milliseconds?=?TotalMilliseconds?%?1000;return?timeSpan;}
?
?
獲取當前時間
function getNowFormatDate() {var date = new Date();var seperator1 = "-";var seperator2 = ":";var month = date.getMonth() + 1;var strDate = date.getDate();if (month >= 1 && month <= 9) {month = "0" + month;}if (strDate >= 0 && strDate <= 9) {strDate = "0" + strDate;}var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;+ " " + date.getHours() + seperator2 + date.getMinutes()+ seperator2 + date.getSeconds();return currentdate; }?
?
對時間進行加減操作
var date = new Date();date.setDate(date.getDate() + 60);//這里的60就是你要加的天數,減也可以。年、月會相應加上去,值得注意的是date.getMonth()得到的月份比實際月份小1,所以實際月份是(date.getMonth()+1) alert(date.getFullYear() +"-"+ (date.getMonth()+1) +"-"+ date.getDate());?
轉載于:https://www.cnblogs.com/longling2344/p/5341365.html
總結
以上是生活随笔為你收集整理的javascript对时间的校验操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP实现图片压缩的两则实例(转)
- 下一篇: MySQL命令行下执行.sql脚本详解