从日期字符串中获取日期

时间:2015-07-09 18:28:34

标签: javascript date

如何从JavaScript中的日期字符串获取日期?

Output:
2015-06-25 - Thursday
2015-07-10 - Friday

4 个答案:

答案 0 :(得分:2)

你可以试试这个:

var d = new Date("2015-06-25");
var weekday = [];
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var day = weekday[d.getDay()]; // Wednesday

答案 1 :(得分:0)

var dateParts = "2015-06-25".split('-');
(["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"])[new Date(dateParts[0], dateParts[1] - 1, dateParts[2], 0, 0, 0).getDay() - 1]

答案 2 :(得分:0)

var daysOfTheWeek = ['Sun','Mon','Tues','Wed','Thu','Fri','Sat'];
var date = new Date("2015-06-25");
var day = daysOfTheWeek[ date.getDay() ];

答案 3 :(得分:0)

getDay() - 方法返回星期几,表示为从0Sunday)到6Saturday)的整数。

function getWeekDay(dateString){
    var date = new Date(dateString);
    var weekdays = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
                     ^                                       ^                   ^
               index 0                                       4                   6

    return weekdays[date.getDay()];
}        
getWeekDay("2015-06-25"); // calling getWeekDay method
// return day: Thursday