javascript - convert DD/MM/YYYY to YYYYMMDD to compare it with a given string -
i wanted convert date string removing "/" , make yyyymmdd compare first 6 characters of given string.
html:
<div class="cust-dob"> <span class="input-append date form-control2" id="dp3" data-date="12-02-2012" data-date-format="dd-mm-yyyy" placeholder=""> <input class="span2 valid" size="9" name="custdob" id="custdob" type="text" placeholder="dob" value="" readonly="" aria-invalid="false"> <span class="add-on"><i class="fa fa-calendar" aria-hidden="true"></i></span> </span> </div> <input type="text" class="form-control2 resizeselect" name="nric" id="nric" placeholder="" size="12" value="">
script:
$("#nric").blur(function(){ var ic = $("#nric").val(); var dob = $("#cust-dob").val(); var updateddob = new date(dob.split("/").join("")); if(ic != updateddob){ alert("wrong nric"); } console.log(updateddob); });
please me fix issue.
i think want compare 2 differently formatted date strings. have 2 ways can this:
keep string operations
use moment.js
you seem trying use date conversion , string operations @ same time, can add confusion. keep simple.
code snippet:
var = '20111031'; var b = '31/10/2011'; // string operations: console.log( b.split('/')[0] === a.split('').slice(6).join('') && b.split('/')[1] === a.split('').slice(4, 6).join('') && b.split('/')[2] === a.split('').slice(0, 4).join('')); // true // moment.js: console.log(moment(b, 'dd/mm/yyyy').format() === moment(a, 'yyyymmdd').format()) // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
Comments
Post a Comment