1

I have a string that has a date in it and I wan't to be able to convert it.

var startDate = "March-09-2010"; 
var convertedStartDate = new Date(startDate); 
var month = convertedStartDate.getMonth() + 1 
var day = convertedStartDate.getDay(); 
var year = convertedStartDate.getFullYear(); 
var shortStartDate = month + "-" + day + "-" + year; 
alert(shortStartDate);

I want it so it converts March-09-2010 to 09-03-10 (DD-MM-YY)

Anyone know what I am doing wrong?

7 Answers 7

1
var startDate = "March-09-2010"; 
var convertedStartDate = new Date(startDate.replace(/-/g, "/")); // replace hyphen with slash
var month = convertedStartDate.getMonth() + 1 
var date = convertedStartDate.getDate(); 
var year = convertedStartDate.getFullYear(); 
var shortStartDate = date + "-" + month + "-" + year; 
alert(shortStartDate);

demo: http://jsfiddle.net/BjnBW/

Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't work. "March/09/2010" is still not a valid date string. "March, 9 2010" is a valid date string.
1

Try this:

var dt=Date.parse(Yourstring);

formatDate('DD-MM-YY',dt);

Please check this Date.parse

3 Comments

Object function Date() { [native code] } has no method 'Parse'...thats the error
@RajatSinghal Sorry Edited the answer.
1. Object 1268089200000 has no method 'tostring'. 2. after fix: toString() radix argument must be between 2 and 36.
0

Check your syntax changed your code a little, modify it according to it then ---

var startDate = "March/09/2010";
var convertedStartDate = new Date(startDate);
var month = convertedStartDate.getMonth() + 1
var day = convertedStartDate.getDate();
var year = convertedStartDate.getFullYear();
var shortStartDate = day+ "-" + month+ "-" + year;
alert(shortStartDate);

1 Comment

Problem is in the startDate not matching the criteria
0

your date string is not in the correct format. for correct formats, please see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date

try this or jsfiddle

var startDate = "March-09-2010";
var tmp = startDate.split('-');
tmp.splice(1, 0, ',');
var convertedStartDate = new Date(tmp.join(' '));
var month = convertedStartDate.getMonth() + 1
var day = convertedStartDate.getDate();
var year = convertedStartDate.getFullYear();
var shortStartDate = ('0' + day).slice(-2) + "-" + ('0' + month).slice(-2) + "-" + year;
alert(shortStartDate);

Comments

0
var shortStartDate = 
  Globalize.format(Globalize.parseDate(startDate, 'MMMM-dd-yyyy'), 'dd-MM-yy');

Use some library to do the conversion, because the built-in Date.parse() is implementation-dependent. It depends on the system locale what formats it accepts.

The code above uses Globalize.js, which can handle a large number of date formats, including formats with month names in different languages (the default being English).

Comments

0

You'll need to convert 'March' to a number. One way is to use this Array extension to be able to retrieve a month number from a month name:

Array.prototype.enum = function(){
     var obj = {};
     for (var i=0; i<this.length; (i+=1)) {
        obj[this[i]] = i;
     }
     this.enum = obj;
     return this;
};

Now, create an Array with month names

var months = ('January,February,March,April,May,June,July,'+
             'August,September,October,November,December').split(',')
              .enum();

Now you rewrite your date:

var startDate = "March-09-2010".split(/\-/),
    month     = months.enum[startDate[0]]+1;
startDate     = [startDate[1],
                 month < 10 ? '0'+month : month,
                 startDate[2]].join('-');
//=> startDate now is: '09-03-2010'

Comments

0

Use getDateFromFormat() to convert string to date in javascript.

Check this link for more help: http://www.mattkruse.com/javascript/date/

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.