Convert a yyyy-mm-dd to other date format in Javascript - Stack Overflow

I am trying to convert "1983-24-12" to December 24, 1983 using Javascript. Do we have any rea

I am trying to convert "1983-24-12" to December 24, 1983 using Javascript.

Do we have any ready made function in Javascript which makes this conversion same as simpledateformat in Java?

I am trying to avoid taking months in Arrays and then do the manipulation.

My Code is as below.

//For Date Format
    months = ["", "January", "February", "March", "April", "May", "June",           "July", "August", "September", "October", "November", "December"];     
    dob = responseObj.data.personDetail.birthDate.split('-');   
    month = months[dob[1]];
    responseObj.data.personDetail.birthDate = month +" "+dob[2] +","+dob[0];

Kindly help.

I am trying to convert "1983-24-12" to December 24, 1983 using Javascript.

Do we have any ready made function in Javascript which makes this conversion same as simpledateformat in Java?

I am trying to avoid taking months in Arrays and then do the manipulation.

My Code is as below.

//For Date Format
    months = ["", "January", "February", "March", "April", "May", "June",           "July", "August", "September", "October", "November", "December"];     
    dob = responseObj.data.personDetail.birthDate.split('-');   
    month = months[dob[1]];
    responseObj.data.personDetail.birthDate = month +" "+dob[2] +","+dob[0];

Kindly help.

Share edited Dec 24, 2011 at 1:29 kapa 78.8k21 gold badges165 silver badges178 bronze badges asked Dec 24, 2011 at 1:06 GendafulGendaful 5,80213 gold badges59 silver badges78 bronze badges 1
  • There is a Date.prototype.toLocaleString method, but then you'd first have to convert the string to a date (not hard, but needs 2 or 3 lines of code) and then a call to toLocaleString. And then the method is implementation dependent so you get whatever the browser returns, which is different in just about every browser. Many also ignore local settings so return the US-centric month, day, year even if the system settings are for day, moth, year. So the method you are using is already probably as good as it gets. – RobG Commented Dec 24, 2011 at 5:06
Add a ment  | 

4 Answers 4

Reset to default 7

If you don't want to use a library why not just split the date string and concoct your own date?

var str_date = "1983-24-12",
    arr_date = str_date.split('-'),
    months   = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

alert(months[arr_date[2]] + ' ' + arr_date[1] + ', ' + arr_date[0]);

Demo: http://jsfiddle/9xeBZ/

JavaScript has the Date() object: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Date

The problem is that the JavaScript Date() object will not return a string representation of a month. So at some point you will have to create that logic yourself. Also the format you posted ("1983-24-12") is an invalid format for parsing with the Date() object.

EDIT: Since you mentioned that you're using Sencha, you can use Ext.Date to add advanced date formatting and parsing:

A set of useful static methods to deal with date Note that if Ext.Date is required and loaded, it will copy all methods / properties to this object for convenience.

You can find more information here: http://docs.sencha./ext-js/4-0/#!/api/Ext.Date

The two methods you might find useful are parse and format


Alternatively, JQuery UI's DatePicker widget implements a very robust date parser and formatter, which can be used in your own code like this:

var parsedDate = $.DatePicker.parseDate("yy-m-d", "1983-24-12");
var outputString = $.DatePicker.formatDate("MM d, yy", parsedDate);
// outputString = "December 24, 1983"

The relevant functions here are parseDate and formatDate.

There is one problem though, to use this you'll have to include both JQuery and JQuery UI as scripts in your code, which is probably a little excessive for what you need, and the other answers may perhaps suit your needs better.

This might help: Date Format Conversion

If I understand you right, you are trying to reformat your date for which a quick google search brought up this http://blog.stevenlevithan./archives/date-time-format which looks like what you want.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743679003a4489054.html

相关推荐

  • Convert a yyyy-mm-dd to other date format in Javascript - Stack Overflow

    I am trying to convert "1983-24-12" to December 24, 1983 using Javascript. Do we have any rea

    3天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信