1

I have a string like

15 Feb, 2016

now i want 2016,02,15

0

5 Answers 5

3

You can use Date function

var d = new Date("15 Feb, 2016");
alert(d.getFullYear()+","+(d.getMonth()+1)+"," + d.getDate());
Sign up to request clarification or add additional context in comments.

1 Comment

Parsing of strings is almost entirely implementation dependent. That any ECMAScript implementation should parse that is chance.
2

You could look at a library like Moment to make this easier. Then this code will format it how you want:

var myDate = "15 Feb, 2016";
moment(myDate, "DD MMM, YYYY").format("YYYY,MM,DD")

You can see in another answer how to do it with pure javascript, but Moment will save you some work and also make it quite easy to change your final format if you decide you want something different later.

Comments

1

You could create a date instance and then use it like this...

var date = new Date('15 Feb, 2016');
var dateString = date.getFullYear() + ', ' + (date.getMonth() + 1) + ', ' + date.getDate();

There are also libraries that could help with other formats:

MomentJS date.format

Comments

0

you first need to parse this string into a Date object

var strDate = "15 Feb, 2016";
var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];
var dateFields = strDate.split( " " ).map( function(value){ if ( value.indexOf( "," ) != -1){ return value.substring(0, value.length - 1); } return value; } );
var date = new Date();
date.setDate(dateFields[0]);
date.setMonth(months.indexOf(dateFields[1]));
date.setFullYear( dateFields[2] );

Now you can display it in whichever format you want

alert( date.getFullYear() + "," + (date.getMonth() + 1) + "," + date.getDate() );

This only gives the value in single digit if they are in single digit, so use this method for padding

function pad(var value) {
    if(value < 10) {
        return '0' + value;
    } else {
        return value;
    }
}
alert( date.getFullYear() + "," + pad(date.getMonth() + 1) + "," + pad(date.getDate()) );

Comments

0

While you do need to parse the string, there's no need to create a Date object. All you need to do is get the parts, map the month name to a number and write a formatted string.

The parsing part is the same as parsing it for a Date, but formatting the output string is much simpler:

// Reformat date string like d mmm, y to y, m, d
function reformatDateString(s) {
  var months = {jan:'01',feb:'02',mar:'03',apr:'04',may:'05',jun:'06',
                jul:'07',aug:'08',sep:'09',oct:'10',nov:'11',dec:'12'};
  var b = s.match(/\w+/g);
  return b && b[2] + ',' + months[b[1].toLowerCase()] + ',' + b[0];
}

document.write(reformatDateString('15 Feb, 2016'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.