Created
May 7, 2013 12:02
-
-
Save dvidsilva/5532074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Colocar clase date-input | |
$(function(){ | |
$('.date-input').attr('placeholder','AAAA-MM-DD').on('change', function(e){ | |
var dt=$(this).val(); | |
if(dt!='') | |
{ | |
var da= dt.split('-'); | |
var l = da.length; | |
if(l <= 3) | |
{ | |
var date=''; | |
for(i=0;i<l;i++) | |
{ | |
if(i==0) | |
{ | |
if(da[i].match(/^\d{4}$/)) | |
{ | |
date = da[i] + "-"; | |
} | |
else date = new Date().getFullYear() + "-"; | |
} | |
if(i==1) | |
{ | |
if(da[i].match(/^\d{1}|d{2}$/) && da[i]>0 && da[i]<=12) | |
{ | |
var m = da[i].length==1 ? '0'+da[i] : da[i]; | |
date += m + "-"; | |
} | |
} | |
if(i==2) | |
{ | |
if(da[i].match(/^\d{1}|d{2}$/) && da[i]>0 && da[i]<=31) | |
{ | |
var d = da[i].length==1 ? '0'+da[i] : da[i]; | |
date += d; | |
} | |
} | |
} | |
if(l<3) | |
{ | |
date+='/'+new Date().getFullYear(); | |
} | |
if(date.match(/^\d{4}\/\d{2}\/\d{2}$/)) | |
{ | |
$(this).val(date); | |
} | |
else | |
{ | |
var month = dateObj.getUTCMonth(); | |
var day = dateObj.getUTCDate(); | |
var year = dateObj.getUTCFullYear(); | |
newdate = year + "-" + month + "-" + day; | |
$(this).val(date); | |
}; | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment