我需要能够获得以下内容:
11 MAR
我尝试了以下内容:
function changeDate(date)
{
var d = new Date();
if(d) // check if in the current week
{
return date.format(EEE);
}else if() //check if out of the week
{
return date.format(d MMM);
}
}
date
的格式为MM/dd/yyyy HH:mm
答案 0 :(得分:0)
假设当前日期表示给定日期
试试这个:
function getWeek(d) {
var target = new Date(d.valueOf());
var dayNr = (d.getDay() + 6) % 7;
target.setDate(target.getDate() - dayNr + 3);
var jan4 = new Date(target.getFullYear(), 0, 4);
var dayDiff = (target - jan4) / 86400000;
var weekNr = 1 + Math.ceil(dayDiff / 7);
return weekNr;
}
function changeDate(date) {
var d = new Date();
return getWeek(d) == getWeek(date)?
date.toString().substring(0,3).toUpperCase():
b.getDate() + " " + b.toString().substring(3,7).toUpperCase();
}
其中getWeek()
取自here。
样本用法:
var a = new Date("Feb 14 2014"),
b = new Date("Mar 15 2014");
changeDate(a); // "FRI"
changeDate(b); // "15 MAR"
答案 1 :(得分:0)
此方法getCalWeek
将为您提供一个包含给定日期的周和年的对象。
function thursday(mydate) {
var Th=new Date();
Th.setTime(mydate.getTime() + (3-((mydate.getDay()+6) % 7)) * 86400000);
return Th;
}
function getCalWeek(thedate) {
ThursDate=thursday(thedate);
weekYear=ThursDate.getFullYear();
ThursWeek1=thursday(new Date(weekYear,0,4));
theweek=Math.floor(1.5+(ThursDate.getTime()-ThursWeek1.getTime())/86400000/7);
return {"week": theweek, "year" : weekYear};
}
然后你可以比较两个日期的周和年
var weekdays = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
var months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
function changeDate(date) {
d = new Date(); // today
weekAndYearToday = getCalWeek(d);
weekAndYearDate = getCalWeek(date);
if(weekAndYearToday.week = weekAndYearDate.week && weekAndYearToday.year = weekAndYearDate.year) {
// here it is clear that "date" is in the current week
return weekdays[date.getDay()];
} else {
return date.getDate() + " " + months[date.getMonth()];
}
}
答案 2 :(得分:0)
当您提供验证日期和要测试的周时,下面的代码应该为您提供所需的日期。
已经过单元测试here。
function changeDate(dateToVerify, dateToVerifyAgainst)
{
var dateToVerify = new Date(dateToVerify);
var dateToVerifyAgainst = new Date(dateToVerifyAgainst);
function isInRequiredWeek(dateToVerify, dateToVerifyAgainst) {
var dateToVerify = new Date(dateToVerify);
var dateToVerifyAgainst = new Date(dateToVerifyAgainst);
function getMonday(d) {
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
return new Date(d.setDate(diff));
}
var startOfWeek = getMonday(dateToVerifyAgainst);
startOfWeek.setHours(0);
startOfWeek.setMinutes(0);
startOfWeek.setSeconds(0);
var endOfWeek = new Date(dateToVerifyAgainst);
endOfWeek.setHours(0);
endOfWeek.setMinutes(0);
endOfWeek.setSeconds(0);
endOfWeek.setDate(endOfWeek.getDate() + 6);
endOfWeek.setHours(23);
endOfWeek.setMinutes(59);
endOfWeek.setSeconds(59);
endOfWeek.setMilliseconds(999);
if ( dateToVerify >= startOfWeek && dateToVerify <= endOfWeek) {
return true;
} else {
return false;
}
}
Date.prototype.getDayAbbr = function(){
var days_abbr = [
'SUN',
'MON',
'TUE',
'WED',
'THU',
'FRI',
'SAT'
];
return days_abbr[this.getDay()];
};
Date.prototype.getMonthAbbr = function(){
var month_abbrs = [
'JAN',
'FEB',
'MAR',
'APR',
'MAY',
'JUN',
'JUL',
'AUG',
'SEP',
'OCT',
'NOV',
'DEC'
];
return month_abbrs[this.getMonth()];
};
if (isInRequiredWeek(dateToVerify, dateToVerifyAgainst)) // check if in the current week
{
return dateToVerify.getDayAbbr();
}else
{
return (dateToVerify.getDate() + " " + dateToVerify.getMonthAbbr());
}
}