<script language=javascript>
// 86,400,000 (24* 60 * 60*1000)
//
function getWeekOfYear(date) {
var d1 = date;
var d2 = new Date(date.getFullYear(), 0, 1);
var d = Math.round((d1 - d2) / 86400000);
return Math.ceil((d + ((d2.getDay() + 1) - 1)) / 7);
};
// create by lishaodng
function getStartAndEndByWeek(year,week){
var Nowdate=new Date(year, 0, 1);
var nowweek_start=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
var nowweek = 1;
var start = new Date(nowweek_start-86400000*7*(nowweek-week)).format("yyyy-MM-dd");
var end = new Date(nowweek_start-86400000*7*(nowweek-week) + 86400000*6).format("yyyy-MM-dd");
return start + ' ' + end;
}
//
function showWeekFirstDay()
{
var Nowdate=new Date();
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
return WeekFirstDay;
}
//
function showWeekLastDay()
{
var Nowdate=new Date();
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
return WeekLastDay;
}
//
function showPreviousFirstWeekDay(firstDay)
{
var WeekFirstDay=(firstDay==null?showWeekFirstDay():firstDay)
return new Date(WeekFirstDay-86400000*7)
}
//
function showPreviousLastWeekDay(firstDay)
{
var WeekFirstDay=(firstDay==null?showWeekFirstDay():firstDay)
return new Date(WeekFirstDay-86400000)
}
//
function showNextFirstWeekDay(lastDay)
{
var MonthFirstDay=(lastDay==null?showWeekLastDay():lastDay)
return new Date((MonthFirstDay/1000+86400)*1000)
}
//
function showNextLastWeekDay(lastDay)
{
var MonthFirstDay=(lastDay==null?showWeekLastDay():lastDay)
return new Date((MonthFirstDay/1000+7*86400)*1000)
}
//
Date.prototype.getWeekOfYear = function(weekStart) { // weekStart: : :0, :1, :2 ...,
weekStart = (weekStart || 0) - 0;
if(isNaN(weekStart) || weekStart > 6)
weekStart = 0;
var year = this.getFullYear();
var firstDay = new Date(year, 0, 1);
var firstWeekDays = 7 - firstDay.getDay() + weekStart;
var dayOfYear = (((new Date(year, this.getMonth(), this.getDate())) - firstDay) / 86400000) + 1;
return Math.ceil((dayOfYear - firstWeekDays) / 7) + 1;
}
Date.prototype.format=function(fmt) {
var o = {
"M+" : this.getMonth()+1, //
"d+" : this.getDate(), //
"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //
"H+" : this.getHours(), //
"m+" : this.getMinutes(), //
"s+" : this.getSeconds(), //
"q+" : Math.floor((this.getMonth()+3)/3), //
"S" : this.getMilliseconds() //
};
var week = {
"0" : "\u65e5",
"1" : "\u4e00",
"2" : "\u4e8c",
"3" : "\u4e09",
"4" : "\u56db",
"5" : "\u4e94",
"6" : "\u516d"
};
if(/(y+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
if(/(E+)/.test(fmt)){
fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "\u661f\u671f" : "\u5468") : "")+week[this.getDay()+""]);
}
for(var k in o){
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
function getWeeks(num, obj){
var firstDay = showWeekFirstDay();
var lastDay = showWeekLastDay();
var divContent = "";
for(var i=0;i<num;i++){
lastDay = showPreviousLastWeekDay(firstDay);
firstDay = showPreviousFirstWeekDay(firstDay);
var year = lastDay.getFullYear();
var week = lastDay.getWeekOfYear(1);
divContent = "<option year='"+year+"' week='"+week+"'>"+year+" " +week+ " ("+firstDay.format("yyyy-MM-dd")+" "+lastDay.format("yyyy-MM-dd")+")</option>"+divContent;
}
firstDay = showWeekFirstDay();
lastDay = showWeekLastDay();
for(var i=0;i<num;i++){
var year = lastDay.getFullYear();
var week = lastDay.getWeekOfYear(1);
if(i!=0) {
divContent = divContent +"<option year='"+year+"' week='"+week+"'>"+ year+" "+ week +" ("+firstDay.format("yyyy-MM-dd")+" "+lastDay.format("yyyy-MM-dd")+")</option>";
} else {
divContent = divContent +"<option year='"+year+"' week='"+week+"'>"+ year+" "+ week +" ("+firstDay.format("yyyy-MM-dd")+" "+lastDay.format("yyyy-MM-dd")+")</option>";
}
firstDay = showNextFirstWeekDay(lastDay);
lastDay = showNextLastWeekDay(lastDay);
}
divContent = "<select id='yearWeekSelect' onchange='changeYearWeek(this);'>"+divContent+"</select>"
obj.html(divContent);
}
// year、month
function changeYearWeek(obj){
var $option = $(obj).children("option[selected]");
var year = $option.attr("year");
var week = $option.attr("week");
$("#_year").val(year);
$("#_week").val(week);
}
function changeSelected() {
var _year = $("#_year").val();
var _week = $("#_week").val();
$("#yearWeekSelect").children("option").each(function(){
if($(this).attr("year") == _year && $(this).attr("week") == _week) {
$(this).attr("selected", true);
}
})
}
//alert("ss:"+(new Date(2012,0,1).getWeekOfYear(1)));
//new Date(2012,0,1).getWeekOfYear(1)
</script>
<script>
/**
* 、 、 、 、
*/
var now = new Date(); //
var nowDayOfWeek = now.getDay(); //
var nowDay = now.getDate(); //
var nowMonth = now.getMonth(); //
var nowYear = now.getYear(); //
nowYear += (nowYear < 2000) ? 1900 : 0; //
var lastMonthDate = new Date(); //
lastMonthDate.setDate(1);
lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
var lastYear = lastMonthDate.getYear();
var lastMonth = lastMonthDate.getMonth();
// :yyyy-MM-dd
function formatDate(date) {
var myyear = date.getFullYear();
var mymonth = date.getMonth()+1;
var myweekday = date.getDate();
if(mymonth < 10){
mymonth = "0" + mymonth;
}
if(myweekday < 10){
myweekday = "0" + myweekday;
}
return (myyear+"-"+mymonth + "-" + myweekday);
}
//
function getMonthDays(myMonth){
var monthStartDate = new Date(nowYear, myMonth, 1);
var monthEndDate = new Date(nowYear, myMonth + 1, 1);
var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24);
return days;
}
//
function getQuarterStartMonth(){
var quarterStartMonth = 0;
if(nowMonth<3){
quarterStartMonth = 0;
}
if(2<nowMonth && nowMonth<6){
quarterStartMonth = 3;
}
if(5<nowMonth && nowMonth<9){
quarterStartMonth = 6;
}
if(nowMonth>8){
quarterStartMonth = 9;
}
return quarterStartMonth;
}
//
function getWeekStartDate() {
var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
return formatDate(weekStartDate);
}
//
function getWeekEndDate() {
var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
return formatDate(weekEndDate);
}
//
function getMonthStartDate(){
var monthStartDate = new Date(nowYear, nowMonth, 1);
return formatDate(monthStartDate);
}
//
function getMonthEndDate(){
var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
return formatDate(monthEndDate);
}
//
function getLastMonthStartDate(){
var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
return formatDate(lastMonthStartDate);
}
//
function getLastMonthEndDate(){
var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
return formatDate(lastMonthEndDate);
}
//
function getQuarterStartDate(){
var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
return formatDate(quarterStartDate);
}
//
function getQuarterEndDate(){
var quarterEndMonth = getQuarterStartMonth() + 2;
var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
return formatDate(quarterStartDate);
}
</script>