We help IT Professionals succeed at work.

need to validate date time in tabular format

puneet kumar
puneet kumar asked
on
High Priority
65 Views
Last Modified: 2020-03-09
Hi Team,



I have an issue with date time picker. i have 3 date fields that are Pdate,Qdate,bDate , i put the validation while selecting B Date is greater then Q Date and q Date is greater then P Date and B Date is greater then P Date simple you can say that P Date , Q Date and B Date is in increasing order when i am taking date picker it's working fine but when i am using the date time picker its comparing each column time separatly and checking the time also in increasing order but i want only date compare my code is below please check and let me know if you need more clarification . please help me out on my below code where
i m doing wrong.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.20/jquery.datetimepicker.full.min.js"></script>



<script>
    function validateDates() {
	 //get values of dates
	 var array = [];
	 var error = false;
	 $(".chkValues:checked").each(function() {
	   var parent = $(this).closest('tr');
	   var last = 0;
	   //var lbl = ['Pdate','PPDate','Bdate'];
	   var lbl = ['Pdate','QDate','Bdate'];
	   $('.date', parent).each(function(i, e) {
	     console.log(parent);
	     var dt = new Date(e.value.split("/").reverse().join("-"));
	     console.log(dt);
	     if (dt.getTime() <= last) {
	    	 alert(lbl[i] + ' date must be greater than ' + lbl[i-1] + ' date');
	       error=true;
	     }
	     last = dt.getTime();
	   });
	   $row = $(this).data('row'); //row1, row2, row3 etc
	   $('input.' + $row).each(function() {
	     array.push($(this).val());
	   });
	 });
	 return error ? [] : array;
	}
	
	$(document).ready(function() {
		
	$('#btnSubmit').click(function(evt){
	   evt.preventDefault();
	   
	   const array = validateDates();
	   console.log(array.length)
	   if (array.length === 0) {
	     //alert("Some issue with dates");
	     return;
	   }
	   $.ajax({
	     type: "POST",
	     url: "Test.jsp",
	     data: {
	    	 array : array 
	    	 },

	     success: function(responseFromServer) {
	       $.each(responseFromServer, function(resultMessageKey, resultMessageValue) {
	    	   window.location.reload();
	    	   $('#content').html(resultMessageKey);
	       });
	     },
	     error: function() {
	       alert(" Ajax call Failed to Update Values  into Database ");
	     }
	   });
	 });

	 $(".date").datetimepicker({
		 format:'d/m/Y H:i'
	 });

	 $('.chkValues').on('click', validateDates);
	}); 
  </script>
  </head>
<body>
    <form action="" id="myForm" method="post">
  <table id="tableData" width="100%" name="tableData">
    <tr>
      <th>Check/UnCheck</th>
      <th>MYear</th>
      <th>P Date</th>
      <th>Q Date</th>
      <th>B Date</th>
    </tr>
    <tr>
      <td><INPUT type="checkbox" data-row="row" class="chkValues" name="chkBox" value="" size="50"/></td>
      <td align="left"><input class="inputText row0" type="text" name="mYear" id="mYear0" value="1-2020"></td>
      <td><input class="date row0" id="pDate0" name="dob" type="text" value="24/01/2020 12:50" /></td>
      <td><input class="date row0" id="qDate0" name="dob" type="text" value="26/01/2020 10:50" /></td>
      <td><input class="date row0" id="bDate0" name="dob" type="text" value="28/01/2020 09:40" /></td>
    </tr>
     <tr>
      <td><INPUT type="checkbox" data-row="row" class="chkValues" name="chkBox" value="" size="50"/></td>
      <td align="left"><input class="inputText row1" type="text" name="mYear" id="mYear1" value="2-2020"></td>
      <td><input class="date row1" id="pDate1" name="dob" type="text" value="20/02/2020 08:50" /></td>
      <td><input class="date row1" id="qDate1" name="dob" type="text" value="22/02/2020 11:25" /></td>
      <td><input class="date row1" id="bDate1" name="dob" type="text" value="24/02/2020 05:15" /></td>
    </tr>
    <tr>
      <td><INPUT type="checkbox" data-row="row" class="chkValues" name="chkBox" value="" size="50"/></td>
      <td align="left"><input class="inputText row2" type="text" name="mYear" id="mYear2" value="3-2020"></td>
      <td><input class="date row2" id="pDate2" name="dob" type="text" value="24/03/2020 02:35" /></td>
      <td><input class="date row2" id="qDate2" name="dob" type="text" value="26/03/2020 22:45" /></td>
      <td><input class="date row2" id="bDate2" name="dob" type="text" value="28/03/2020 20:15" /></td>
    </tr>
  </table>
  <input id="btnSubmit" type="submit" value="submit" />
</form>
</body>
</html>

Open in new window

Comment
Watch Question

IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
Seems you just want

var dt = new Date(e.value.split(" ")[0].split("/").reverse().join("-"));
OR if you want the time to count too

var dString = e.value.split(" ");
var dt = new Date(dString[0].split("/").reverse().join("-")+" "+dString[1]+":00"

Author

Commented:
Hi Michel,

it working perfectly thanx a lot , one more issue that in calendar time is showing like 07:00 , 08:00 its hours only can we show in same calendar minutes also .please help me out on this . like 07:01 , 07:02 like this .

Author

Commented:
Hi Michel,

it working perfectly thanx a lot , one more issue that in calendar time is showing like 07:00 , 08:00 its hours only can we show in same calendar minutes also .please help me out on this . like 07:01 , 07:02 like this .
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
step is 60.  https://xdsoft.net/jqplugins/datetimepicker/ 
If you change to step: 1
you will get a very long time select

$(".date").datetimepicker({
  step: 1,
  format: 'd/m/Y H:i'
});

Author

Commented:
Hi Michel,


Can you please update my code regarding this because i checked that still it is coming like 7:10 , 8:10  , 9:10 like this hourly . but i want like 7.10 , 7.11, 7.12 like this please update on my code i m not able to get it and help me out on this many many thanx in advance.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:

Author

Commented:
HI Michel you are to good man thanx a ton for your support and great help . Michel i have 1 more question thread in below link please check if you can do some help of me it would be very great to you.

https://www.experts-exchange.com/questions/29174343/Already-Populated-date-field-date-picker-validation-based-on-specific-month.html

Author

Commented:
HI Michel one more thing in internet explorer this date time picker calendar is not coming please tell me solution for that also it would be a great to you.many many thanx in advance.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Any console errors? I do not have IE here. I can look on Monday

Author

Commented:
Hi Michel i don't get any console error please have a look from your side when you get time . thanks for support.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
I tested it now in IE11 on Win10. The fiddle works as designed for me

https://jsfiddle.net/mplungjan/o2h15gy3/embedded/result,css,html,js

Author

Commented:
Hi Michel IE 8 its not working any idea how can we make it working in IE 8.many may thanx in advance.
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
No. I will not support IE8 in any way anymore. Anyone using it must upgrade

"Support for all versions of IE on Windows XP ended effective April 8, 2014 due to its end-of-life.[8] On January 12, 2016, support for IE8 on all supported Windows operating systems ceased, due to new support policies dictating that only the newest version of IE available for a supported version of Windows is supported. Only Internet Explorer 11 is to be supported, except for Windows Vista and Windows Server 2008, where only Internet Explorer 9 is supported, and Windows Server 2012, where only Internet Explorer 10 is supported."