0

I have a date format as 2016-01-09.Now I want to convert this date format in to 09-Jan-2016 I was able to convert in to 09-01-2016.But how can i get the month name added there.can someone help.. Here is the code which iam trying

function formatDate(input){
            var datePart = input.match(/\d+/g),
            year = datePart[0].substring(0,4), // get only two digits
            month = datePart[1], day = datePart[2];

            return day+'-'+month+'-'+year;
}
formatDate ('2010-01-18'); // "18-01-2010"
Chandana Kumara
  • 2,129
  • 1
  • 17
  • 23
H Varma
  • 440
  • 1
  • 10
  • 25
  • Try with the link below if you want to use only plain javascript [http://stackoverflow.com/questions/27480262/get-current-date-in-dd-mon-yyy-format-in-javascript-jquery](http://stackoverflow.com/questions/27480262/get-current-date-in-dd-mon-yyy-format-in-javascript-jquery) – selvakumar Nov 01 '16 at 05:56

3 Answers3

5

You can do this in multiple ways as follows:

var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function dateFormat(d){
  var t = new Date(d);
  return t.getDate()+'-'+monthNames[t.getMonth()]+'-'+t.getFullYear();
}

OR

You can get month in this way:

var objDate = new Date(date).toLocaleString("en-us", { month: "long" });//January
var objDate = new Date(date).toLocaleString("en-us", { month: "short" }); //Jan

OR

You can use moment.js

var shortMonthName = moment(date).format('MMM') // Jan
var fullMonthName = moment(date).format('MMMM') // January

Refer:http://momentjs.com/

Chandana Kumara
  • 2,129
  • 1
  • 17
  • 23
  • Thanks @chandana Kumara..for many alternatives..your first approach works in my case..But for day it is giving only single digit eg..for 2016-10-09 it is giving as 9-OCT-2016..How can I get 0 added before – H Varma Nov 01 '16 at 06:35
  • Also if the date is in the format i require i dont want to pass to this function.So how can i write the condition that date is in correct format DD-MON-YYYY...Thanks – H Varma Nov 01 '16 at 06:39
  • can u help in this? – H Varma Nov 01 '16 at 06:54
3

Try this

function formatDate(d) 
        {
          var date = new Date(d);

         if ( isNaN( date .getTime() ) ) 
         {
            return d;
         }
         else
        {
          
          var month = new Array();
          month[0] = "Jan";
          month[1] = "Feb";
          month[2] = "Mar";
          month[3] = "Apr";
          month[4] = "May";
          month[5] = "Jun";
          month[6] = "Jul";
          month[7] = "Aug";
          month[8] = "Sept";
          month[9] = "Oct";
          month[10] = "Nov";
          month[11] = "Dec";

          day = date.getDate();
          
          if(day < 10)
          {
             day = "0"+day;
          }
          
          return    day  + " " +month[date.getMonth()] + " " + date.getFullYear();
          }
            
         }


date_response = formatDate('2016-10-25');

alert(date_response)
  • Why you have use so many lines to create simple array. You can do simply it as var month=["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; – Chandana Kumara Nov 01 '16 at 06:01
  • yeah i know but i just coppied that array – Iññoceñt Ùšmâñ Nov 01 '16 at 06:04
  • i have not seen yours . My Connection was slow therefor i was not able to post – Iññoceñt Ùšmâñ Nov 01 '16 at 06:06
  • @IññoceñtÙšmâñ .your approach works in my case..But for day it is giving only single digit eg..for 2016-10-09 it is giving as 9-OCT-2016..How can I get 0 added before – H Varma Nov 01 '16 at 06:44
  • @IññoceñtÙšmâñ can u help me in this? – H Varma Nov 01 '16 at 06:54
  • Thanks @IññoceñtÙšmâñ ..One last ..I dont want to pass the date to this function if the date is already in the required format of DD-MON-YYYY..What should be the condition i should give here...Since here every date is getting called to that function and if the date is already in correct format im getting error – H Varma Nov 01 '16 at 07:14
  • If you do not want to pass the date to this function then you do not need to call this function . If You want to call this function then you have to pass the date – Iññoceñt Ùšmâñ Nov 01 '16 at 07:20
  • @IññoceñtÙšmâñ..The problem here is the dates will come dynamically..I dont have the control to call the function one by one..So I want to validate the date before calling function if the date is correct will not call else will call..So how to give condition – H Varma Nov 01 '16 at 07:26
  • sometimes the date will already be in correct way eg..01-Jan-2016 ..In that case it gives NaN-undefined..So here the date is in correct format,So that i wont call the function..so how can i check this condition..Thanks imp one plz – H Varma Nov 01 '16 at 07:29
  • what is that getTime()? – H Varma Nov 01 '16 at 07:50
  • Return the number of milliseconds since 1970/01/01 for the given date – Iññoceñt Ùšmâñ Nov 01 '16 at 07:52
  • What if other way I want to pass only few which are not in correct format ..Most are in DD-MON-YYYY..Some are in YYYY-MM-DD...In this case i get undefined error – H Varma Nov 01 '16 at 07:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127086/discussion-between-innocent-usman-and-h-varma). – Iññoceñt Ùšmâñ Nov 01 '16 at 08:00
0

Easiest to use a 3rd party library, e.g. momenentjs (npm install moment). You can of course roll it yourself, but the moment/Date format commands are very expressive/powerful. They also support duration and datetime conversions out of the box.

 >>> var moment = require('moment')
 >>> moment('2016-01-09').format('DD-MM-YY')
 '09-01-16'
rmharrison
  • 3,100
  • 1
  • 18
  • 30
  • is this all you do? promote momentjs? – GROVER. Nov 01 '16 at 05:54
  • Can also use Date built-in with more work. My argument is that unless there is a a *compelling* reason, there is simply no point in rolling time/date conversions yourself when there are lightweight 3rd party libs available. Keep it DRY / don't re-invent the wheel. – rmharrison Nov 01 '16 at 06:00
  • something such as this doesn't require a 3rd party library (no matter how `lightweight`). It's literally 3 or 4 lines of JavaScript. As Chandana Kumara has already shown – GROVER. Nov 01 '16 at 06:04
  • Yet @Chandana Kumara still presents the momentjs option (props, but add the full solution of 'DD-MM-YY'). 3-4 lines of stackoverflow will *work* for dev, but are brittle for prod. – rmharrison Nov 01 '16 at 06:13
  • yeah, after all of the other options (which work, btw). tell me how they are brittle, exactly? – GROVER. Nov 01 '16 at 06:14