首页 > 代码库 > javascript 字符串转日期 获取当天是星期几

javascript 字符串转日期 获取当天是星期几

服务器传回日期字符串 格式如下:

2015-01-20 12:03:12
2015-01-19 19:10:00
2015-01-19 12:00:00
2015-01-19 08:41:00
2015-01-16 12:23:46
要求 判断该日期是星期几 转成如下格式:

技术分享

首先把字符串转换成日期格式 查了一些方法 下面这个最简单

var str = '2015-01-20 12:03:12';
dateStr = dateStr.replace(/-/g,'/');
var date = new Date(dateStr);
OK.转成日期格式后 调用getDay()方法就能判断是周几了

var day = date.getDay();

完整方法如下:

function getDayOfWeek(dateStr){
	var dayOfWeek = "";
	dateStr = dateStr.replace(/-/g,'/');
	var date = new Date(dateStr);
	switch(date.getDay()){
		case 0:
			dayOfWeek = '周日';
			break;
		case 1:
			dayOfWeek = '周一';
			break;
		case 2:
			dayOfWeek = '周二';
			break;
		case 3:
			dayOfWeek = '周三';
			break;
		case 4:
			dayOfWeek = '周四';
			break;
		case 5:
			dayOfWeek = '周五';
			break;
		case 6:
			dayOfWeek = '周六';
			break;
	}
	return dayOfWeek;
}
O(∩_∩)O~ 

javascript 字符串转日期 获取当天是星期几