-1

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

Community
  • 1
  • 1
super cool
  • 6,003
  • 2
  • 30
  • 63
  • Are you against just doing the conversion manually? It wouldn't take many lines of code. – Clint Powell Nov 28 '14 at 18:00
  • oh guys common enough sarcasm to down vote . i'm pretty sure i am clear about my question . cheers . – super cool Nov 28 '14 at 18:08
  • 1
    Dates and times can be tricky. Even though you mentioned you don't want plugins, you might consider adding a date library (like [http://momentjs.com/](momentjs)) and let it take care of the date formatting for you. Then you can avoid thinking about things like timezones and daylight savings and leap years. – mr rogers Nov 28 '14 at 18:08
  • @ClintPowell it will do it but in a hard way right mate where there is always a chance of code breaking . can you walk me through your approach ? – super cool Nov 28 '14 at 18:10
  • @mrrogers i been using knockout with jquery but if i use this moment.js which will be a issue for me as i already tried this wonderful plugin . for display thumbs up but other way it fails – super cool Nov 28 '14 at 18:28

1 Answers1

1

Here's a quick and simple way to do what you want. It's just a custom method. Sometimes it's best to take a simple homemade solution. http://jsfiddle.net/jdkkruap/6/

function customDate(d, monthFirst) {
    if(!(d instanceof Date)) {
        !monthFirst ? d = new Date(d.replace(/(\d+)([-\/\\\.])(\d+)/, "$3$2$1")) : d = new Date(d);
    }
    var months = ["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"];
    return (d.getDate() > 9 ? d.getDate() : "0"+d.getDate()) +'-'+months[d.getMonth()]+'-'+(d.getFullYear()+"").substr(2)
}

Now you can call the method as follows:

customDate('10-12-2014');       //output will be 10-dec-14
customDate('10-12-2014', true); //output will be 12-oct-14

If you really want flexibility, consider using moment.js: http://momentjs.com/docs/#/displaying/

Clint Powell
  • 2,368
  • 2
  • 16
  • 19