Javascript Data Type How to - Switch date formats of yyyy-mm-dd and dd/mm/yyyy to MS Access DB format of mm/dd/yyyy








Question

We would like to know how to switch date formats of yyyy-mm-dd and dd/mm/yyyy to MS Access DB format of mm/dd/yyyy.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from ww  w  . j  av a2  s.  c  o  m-->
function parseDate(date) {
    if ( date.indexOf('-') != -1) {
        var arr = date.split('-');
        return arr[1] + '/' + arr[2] + '/' + arr[0]
    }else{
        var arr = date.split('/');
        return arr[1] + '/' + arr[0] + '/' + arr[2]        
    }
}
$('body').append('2014-10-22 -> ' + parseDate('2014-10-22'));
$('body').append('<br>')
$('body').append('31/01/2012 -> ' + parseDate('31/01/2012'));
});
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: