JavaScript日期转换器

时间:2016-04-02 10:22:25

标签: javascript datetime-format

我正在使用Yahoo YQL API来获取即将到来的板球比赛列表,现在的问题是 - 日期格式是非常不同的。我无法使用JavaScript转换日期格式。

从雅虎收到的日期格式:2016-04-03T19:00:00 + 05:30
我需要的日期格式: 2016年4月3日星期一

3 个答案:

答案 0 :(得分:0)

您可以使用Date.parse()来解析日期。

var date = Date.parse("2016-04-03T19:00:00+05:30");

您可以通过调用Date的成员手动设置日期格式:

var days= ["Mon", "Thu", "Wed", "Thu", "Fri", "Sat", "Sun"];
var months = [ ... ];

var result = [];

// Day of week:
result.push(days[date.getDay()]); 

result.push(", ");

// Day of month:
result.push(date.getDate());

result.push(" ");

// Month:
result.push(months[date.getMonth()]); // Note that getMonth() is zero-based.

result.push(" ");

// Year:
result.push(date.getYear());

console.log(result.join(""));

答案 1 :(得分:0)

var d = new Date('2016-04-03T19:00:00+05:30');

将d返回为

Sun Apr 03 2016 19:00:00 GMT+0530 (IST)

现在使用函数来获取

之类的元素
d.getDate() returns 3

Date functions

答案 2 :(得分:0)

试试这个:

var d = new Date('2016-04-03T19:00:00+05:30');
document.write(d.toUTCString().slice(0,16));

参考:http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_toutcstring