10 examples of 'javascript check date' in JavaScript

Every line of 'javascript check date' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1function checkdate (m, d, y) {
2 // discuss at: http://phpjs.org/functions/checkdate/
3 // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4 // improved by: Pyerre
5 // improved by: Theriault
6 // example 1: checkdate(12, 31, 2000);
7 // returns 1: true
8 // example 2: checkdate(2, 29, 2001);
9 // returns 2: false
10 // example 3: checkdate(3, 31, 2008);
11 // returns 3: true
12 // example 4: checkdate(1, 390, 2000);
13 // returns 4: false
14
15 return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0))
16 .getDate()
17}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

347function checkDate(m, d, y) {
348 return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
349}
106function dateHandler(date) {
107 var
108 dArray, // 数组化后的日期
109 ret;
110
111 if (typeof date === 'string') { // 如果date参数是string类型
112 if (rNumberstring.test(date)) { // 如果date参数是number string类型
113 ret = new Date(date);
114 } else { // 这里重新格式化,一般都是从服务端过来的数据,必须有年月日,并且顺序是年月日时分秒毫秒,并且7个值之间有间隔符,间隔符为\D
115 dArray = date.match(rDatestring); // 从string中提取new Date需要的参数
116 if(dArray.length < 3) { // 服务端时间戳,例如NET "/Date(562941040500+0800)/"
117 ret = new Date(+dArray[0]);
118 } else { // 格式化过的
119 ret = new Date(dArray[0], dArray[1] - 1, dArray[2] || 1, dArray[3] || 0, dArray[4] || 0, dArray[5] || 0, dArray[6] || 0);
120 }
121 }
122 } else if (typeof date === 'number' || Object.prototype.toString.call(date) === '[object Date]') { // 如果date参数是number类型、date类型
123 ret = new Date(+date);
124 } else if (date == null) { // 如果不存在date参数
125 ret = new Date();
126 } else {
127 return false;
128 }
129
130 return ret;
131 }
65function getDateObj(myDate: any): Date | null {
66 if (myDate == null) {
67 return null;
68 }
69
70 if (myDate instanceof String || typeof myDate === 'string') {
71 return new Date('' + myDate);
72 }
73
74 // A Firebase Timestamp format
75 if (myDate && (myDate.seconds >= 0 || myDate.seconds < 0) && (myDate.nanoseconds >= 0 || myDate.nanoseconds < 0)) {
76 return new Date(myDate.toDate());
77 }
78
79 return myDate;
80}
27static checkDate(longTime) {
28 return false;
29 let currentDate = new Date();
30 let targetDate = new Date();
31 targetDate.setTime(longTime);
32 if (currentDate.getMonth() !== targetDate.getMonth()) return false;
33 if (currentDate.getDate() !== targetDate.getDate()) return false;
34 if (currentDate.getHours() - targetDate.getHours() > 4) return false;
35 return true;
36}
127function updateDate() {
128 var unix = parseDateString($("#input").val());
129 $('#unix').text(unix);
130 if(unix!==null) {
131 var date = new Date(unix*1000);
132 $('#text').text(date.toString());
133 } else {
134 $('#text').text('');
135 }
136}
269function isMonday(date){
270 var day = new Date (date);
271 timeDiff = checkDST(day);
272 day.setHours(day.getHours() + timeDiff);
273 if(day.getDay() !== 1 && day.getDay() !== 0) {//accepted Sunday for Islamic calendar
274 $('#confirm').text(textSpecifyDay);
275 setTimeout(removeMsg, 4000);
276 return false;
277 };
278 return true;
279};
8function DateCheck(frmMo, frmDy, frmYr)
9{
10 var returnCode;
11 var cdtStr, fdtStr;
12 var cdt = new Date();
13 //cdtStr = cdt.getYear().toString() + ZeroPad(cdt.getMonth() + 1, 2).toString() + ZeroPad(cdt.getDate(), 2).toString()
14 cdtStr = 20070205
15 fdtStr = frmYr + ZeroPad(frmMo, 2) + ZeroPad(frmDy, 2);
16 var frmDate = ZeroPad(frmMo, 2) + '/' + ZeroPad(frmDy, 2) + '/' + frmYr;
17 compareDate = new Date(frmDate);
18 var newDate = ZeroPad(compareDate.getMonth() + 1, 2) + '/' + ZeroPad(compareDate.getDate(), 2) + '/' + frmYr;
19 if (frmDate != newDate)
20 {
21 alert(frmDate + ' is not a valid date!');
22 returnCode = false;
23 }
24 else
25 {
26 if (fdtStr > cdtStr)
27 {
28 alert(frmDate + ' is an invalid BirthDate (greater than current date).\n\nYou have entered: ' + frmDate + ' as a birthdate.\n' + cdtStr + ' is the current date.\n');
29 returnCode = false;
30 }
31 else
32 returnCode = true;
33 }
34
35 return (returnCode);
36}
35export function dateInputCheckInput(evt) {
36 if(!evt) {
37 evt = window.event;
38 if(!evt) {
39 return;
40 }
41 }
42 let c = evt.target || evt.srcElement;
43 dateInputRepairValueIn(c);
44}
32function checkDay(){
33 var pre_time = localStorage['today'];
34 var now_time = new Date().getDay();
35 if (pre_time == now_time){
36 return false;
37 }else{
38 return now_time;
39 }
40}

Related snippets