最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:55:54
文檔

JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例

JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例:本文實(shí)例講述了JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法。分享給大家供大家參考,具體如下: 在項(xiàng)目中用到的,用js 來(lái)得到年月日,星期,當(dāng)前多少個(gè)周的函數(shù),備忘在這里,以后參考。 Date.prototype.getWeek = function(){ var firstDay
推薦度:
導(dǎo)讀JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例:本文實(shí)例講述了JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法。分享給大家供大家參考,具體如下: 在項(xiàng)目中用到的,用js 來(lái)得到年月日,星期,當(dāng)前多少個(gè)周的函數(shù),備忘在這里,以后參考。 Date.prototype.getWeek = function(){ var firstDay

本文實(shí)例講述了JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法。分享給大家供大家參考,具體如下:

在項(xiàng)目中用到的,用js 來(lái)得到年月日,星期,當(dāng)前多少個(gè)周的函數(shù),備忘在這里,以后參考。

Date.prototype.getWeek = function(){
 var firstDayOfYear = new Date(this.getFullYear(),0,1);
 var millOfDay = 24*60*60*1000;
 return Math.ceil((((this - firstDayOfYear) / millOfDay) + firstDayOfYear.getDay()+1)/7);
};
/*
01 October 2013-10:20:20
01 November 2013-10:20:20
01 December 2013-10:20:20
08 October 2013-01:55 pm
*/
var monthMapping = {
 "January" : "01",
 "February" : "02",
 "March" : "03",
 "April" : "04",
 "May" : "05",
 "June" : "06",
 "July" : "07",
 "August" : "08",
 "September" : "09",
 "October" : "10",
 "November" : "11",
 "December" : "12"
};
function formatAmPm() {
 var dateTimeStr = arguments[0] ; 
 var dateStr = dateTimeStr.split("-");
 if (lang.currentLang == "zh_CN" || lang.currentLang == "zh_TW"){
 var s_year = lang.getMessage('yearCn') ;
 var s_month = lang.getMessage('monthCn') ;
 var s_day = lang.getMessage('dayCn') ;
 var dateArray = dateStr[0].split(" ") ;
 var s_time = dateStr[1].split(" ")[1] == "am" ? lang.getMessage('amCn') :lang.getMessage('pmCn') ;
 var ret = dateArray[2] + s_year + monthMapping[dateArray[1]] + s_month + dateArray[0] + s_day + " " + s_time + dateStr[1].split(" ")[0] ; 
 if ( arguments.length > 1 ){
 /*admin feature ,so using en date*/
 ret = arguments[1] ? dateStr[0] : dateStr[1] ;
 }
 return ret ;
 } 
 else{
 if ( arguments.length > 1 ){
 return arguments[1] ? dateStr[0] : dateStr[1] ; 
 }
 else{
 return dateStr[0] + " " + dateStr[1] ;
 }
 } 
}
function formatDate(dateTime, pattern) {
 if(pattern == "" || pattern == null) {
 pattern = "/";
 }
 var date = new Date(dateTime);
 var year = date.getFullYear();
 var month = date.getMonth()+1;
 month = month <10 ? '0'+month : month;
 var day = date.getDate();
 day = day <10 ? '0'+day : day;
 return month + pattern + day + pattern + year;
}
function formatDateDDMMYYYY(dateTime) {
 var ymd = dateTime.split(" ")[0];
 var dateArray = ymd.split("-");
 return dateArray[2]+"."+dateArray[1]+"."+dateArray[0];
}
/**
* 
* @param dateTime YYYY-MM-DD hh:mm:ss
*/
function formatDateDDMMYY(dateTime){
 var ymd = dateTime.split(" ")[0];
 var dateArray = ymd.split("-");
 return dateArray[2]+"."+dateArray[1]+"."+dateArray[0].substring(2);
}
/**********************calculate date*****************************/
var MONTH = [
 {key:"Jan.",value:1},
 {key:"Feb.",value:2},
 {key:"Mar.",value:3},
 {key:"Apr.",value:4},
 {key:"May.",value:5},
 {key:"Jun.",value:6},
 {key:"Jul.",value:7},
 {key:"Aug.",value:8},
 {key:"Sept.",value:9},
 {key:"Oct.",value:10},
 {key:"Nov.",value:11},
 {key:"Dec.",value:12} 
 ];
var DAYS = [31,28,31,30,31,30,31,31,30,31,30,31];
function getYears(){
 var years = [];
 var date = new Date();
 var year = date.getFullYear();
 var minYear = year - 10; 
 for(var currentYear=minYear;currentYear <= year;currentYear++){
 years.push(currentYear);
 }
 return years;
};
function fillinMonthOptions(id,currentMonth){
 $("#" + id).get(0).options.length = 0;
 for(var i= 0;i<MONTH.length; i++){
 if (MONTH[i].value == currentMonth){
 $("#" + id).append("<option value="+MONTH[i].value+" selected=\"selected\">"+MONTH[i].key+"</option>");
 }
 else{
 $("#" + id).append("<option value="+MONTH[i].value+">"+MONTH[i].key+"</option>");
 }
 }
};
function fillinAllMonthOptions(monthIds,currentMonth){
 //var monthIds = ["loginMonth_1","loginMonth_2","auditMonth_1","auditMonth_2"];
 for (var i=0;i<monthIds.length;i++){
 fillinMonthOptions(monthIds[i],currentMonth);
 }; 
};
function fillinYearOptions(id,years,currentYear){
 $("#" + id).get(0).options.length = 0;
 for(var i= 0;i<years.length; i++){
 if (years[i] == currentYear){
 $("#" + id).append("<option value="+years[i]+" selected=\"selected\">"+years[i]+"</option>");
 }
 else{
 $("#" + id).append("<option value="+years[i]+">"+years[i]+"</option>");
 }
 }
};
function fillinAllYearOptions(yearIds,currentYear){
 //var yearIds = ["loginYear","auditYear"];
 years = getYears();
 for(var i= 0;i<yearIds.length; i++){
 fillinYearOptions(yearIds[i],years,currentYear);
 }
};
function isLeapYear(year){
 return (year % 4 === 0) && (year % 100 !==0) || (year % 400 === 0);
}
/*get days of a month*/
function getMonthDays(year,month){
 var monthDays = DAYS[month-1];
 if (isLeapYear(year) && month == 2){
 monthDays = 29;
 }
 return monthDays;
}
function fillinDay(id,monthDays){
 $("#" + id).get(0).options.length = 0;
 for(var i=1;i<=monthDays;i++){
 $("#" + id).append("<option value="+i+">"+i+"</option>");
 }
}
function fiilinDays(year,month,dayIds){
 var monthDays = getMonthDays(year,month);
 for(var i=0;i<dayIds.length;i++){
 fillinDay(dayIds[i],monthDays);
 }
}
function getWeeksOfYear(year){
 var firstDayOfYear = new Date(year,0,1);
 var days = firstDayOfYear.getDay() + (isLeapYear(year) ? 366 : 365);
 return Math.ceil(days/7);
}
function getCurrentWeekNo(){
 var currentDate = new Date();
 var firstDay = new Date(currentDate.getFullYear(),0,1);
 var dayms = 24 * 60 * 60 * 1000;
 var numday = ((currentDate - firstDay) / dayms );
 var weeks = Math.ceil((numday + firstDay.getDay() + 1) / 7);
 return weeks;
}
function getWeekRangeOfWeek(year,weekNo){
 var tmpDate = new Date("1/20/" + year);
 var numOfDaysPastSinceLastMonday = tmpDate.getDay()-1;
 tmpDate.setDate(tmpDate.getDate() - numOfDaysPastSinceLastMonday);
 var weekNoToday = tmpDate.getWeek();
 var weeksInFuture = weekNo - weekNoToday;
 tmpDate.setDate(tmpDate.getDate() + (7 * weeksInFuture) );
 var rangeFrom = tmpDate.getDate() + "/" + MONTH[tmpDate.getMonth()].key;
 tmpDate.setDate(tmpDate.getDate() + 6);
 var rangeTo = tmpDate.getDate() + "/" + MONTH[tmpDate.getMonth()].key ;
 return weekNo + "_#_" + rangeFrom + "_#_" + rangeTo ;
}
function fillinWeek(weekId,year){
 $("#" + weekId).get(0).options.length = 0;
 var weeksOfYear = getWeeksOfYear(year);
 var weeks = [];
 for(var i=0;i<weeksOfYear;i++){
 weeks.push(getWeekRangeOfWeek(year,i+1));
 }
 for(var j=0;j<weeks.length;j++){
 var week = weeks[j].split("_#_");
 $("#" + weekId).append("<option value="+week[0]+">"+week[0] + "(" + week[1]+"-"+week[2]+")</option>");
 }
}
function fillinWeeks(year,weekIds){
 for (var i=0;i<weekIds.length;i++){
 fillinWeek(weekIds[i],year);
 }
}

PS:這里再為大家推薦幾款比較實(shí)用的天數(shù)計(jì)算在線工具供大家使用:

在線日期/天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在線日期計(jì)算器/相差天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/datecalc

在線日期天數(shù)差計(jì)算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

在線天數(shù)計(jì)算器:
http://tools.jb51.net/jisuanqi/datejsq

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript時(shí)間與日期操作技巧總結(jié)》、《JavaScript+HTML5特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例

JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法示例:本文實(shí)例講述了JS實(shí)現(xiàn)處理時(shí)間,年月日,星期的公共方法。分享給大家供大家參考,具體如下: 在項(xiàng)目中用到的,用js 來(lái)得到年月日,星期,當(dāng)前多少個(gè)周的函數(shù),備忘在這里,以后參考。 Date.prototype.getWeek = function(){ var firstDay
推薦度:
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top