I am trying to write down a generic function for my project which returns the date format something like dd-M-y
i.e 10-dec-14
.
Normally i pass input to function parameter as date of format dd-mm-yy
and trying to convert back to my required format .
Function Format(dateval) `04-12-2014`
{
//Trail 1 :
var newdate = new Date(dateval); `o/p: 12 April 2014` like that its getting converted which is way wrong
//Trail 2 :
var newdate = $.DatePicker('dd-M-Y',dateval); `o/p: 12-04-2014`
//Trail 3 : Splitting date and compare and build format manually
return newdate;
}
Its been a hell of experience using new Date
which acts weird many ways something like 13-20-2014
gives jan 20th 2014
phew i can't explain my troubles .
I'm looking for a serious fix on this which does my job in a prolific way .
PS: I'm not a huge fan of plugins . Please don't dig those for my problem .
{EDIT:1}
Possible Solution : Done some digging with reference to answer by Rob post and this wonderful article . Credits to Clint Powell
for his help .
(function() {
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
function fetch(n) {
return n<10? '0'+n : ''+n;
}
function output(dat){
var d = new Date(dat.replace(/(\d+)([-\/\\\.])(\d+)/, "$3$2$1"));
return fetch(d.getDate())+'-'+months[d.getMonth()]+'-'+(d.getFullYear()+'').slice(-2);}
window.output=output; //exposes the output function outside
}());
Functional fiddle Here
Advantage: Best fit if we are trying to do something generic and use in many places
Any suggestion on this is much appreciated