将yyyy-mm-dd转换为Javascript中的其他日期格式 [英] Convert a yyyy-mm-dd to other date format in Javascript
问题描述
我正在尝试使用Javascript将 1983-24-12转换为1983年12月24日。
I am trying to convert "1983-24-12" to December 24, 1983 using Javascript.
我们在Javascript中是否有任何现成的函数可以使此转换与Java中的 simpledateformat
相同?
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.
我的代码如下。
//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];
请帮忙。
推荐答案
编辑:由于您提到正在使用Sencha,因此可以使用 Ext.Date
添加高级日期格式和解析:
Since you mentioned that you're using Sencha, you can use Ext.Date
to add advanced date formatting and parsing:
用于处理日期的一组有用的静态方法请注意,如果需要并加载Ext.Date,它将为方便起见将所有方法/属性复制到该对象。
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.
您可以在此处找到更多信息: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Date
You can find more information here: http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Date
The two methods you might find useful are parse and format
或者,JQuery UI的 DatePicker 小部件实现了非常强大的日期解析器和格式化程序,可以使用在您自己的代码中是这样的:
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"
此处的相关函数是 parseDate 和 formatDate 。
尽管有一个问题,要使用此问题,您必须同时包含JQuery和JQuery UI作为代码中的脚本,这可能会超出您的需要,而其他答案可能会更适合您的需求。
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.
这篇关于将yyyy-mm-dd转换为Javascript中的其他日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!