从这个日期格式dd-mm-yyyy返回日期名称的最佳解决方案是什么?
如果日期是10-03-2014,则脚本将在星期一返回。
moment.js怎么样?
答案 0 :(得分:1)
使用此
var d = new Date();
var dayNames=['mon','tue','wed','thu','fri','sat','sun']
alert(dayNames[d.getDay()-1]);
如果你得到undefine然后你需要做一些改进首先是日期格式 你必须在Date()函数中传递日期之前格式化日期//支持Formate for Date构造函数
//var d = new Date(2011, 01, 07); // yyyy, mm-1, dd
//var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss
//var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss"
//var d = new Date(1297076700000); // milliseconds
//var d = new Date(Feb 07 2011)//mm/dd/yyy
//var d = new Date(16 Mar 2014)//dd/mm/yyy <------USE THIS
//var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC
//var d = new Date("02/07/2011"); // "mm/dd/yyyy" IE8,IE9
现在将代码更改为:
var d = new Date("16 Mar 2014");
var n = d.getDay();
var days = new Array("Sun","Mon","Tue","Wen","Thi","Fri","Sat");
alert(days[n]);
答案 1 :(得分:0)
var date = new Date(2014,02,10);
var day = date.getDay();
日将显示为数字。
然后,您可以使用您的语言构建一个数组来检索日期名称
var daysName = ['Sunday','Monday'...];
alert(daysName[day]);
答案 2 :(得分:0)
这将为您提供完美的输出。
function Day_name(custom_date)
{
var myDate=custom_date;
myDate=myDate.split("-");
var newDate=myDate[2]+"-"+myDate[1]+"-"+myDate[0];
var my_ddate=new Date(newDate).getTime();
var currentDate = new Date(newDate);
var day_name = currentDate.getDay();
var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
return days[day_name];
}
调用此Day_name("02-04-2015");
输出你会得到Thursday