JS实现处理时间,年月日,星期的公共方法示例
本文实例讲述了JS实现处理时间,年月日,星期的公共方法。分享给大家供大家参考,具体如下:
在项目中用到的,用js来得到年月日,星期,当前多少个周的函数,备忘在这里,以后参考。
Date.prototype.getWeek=function(){ varfirstDayOfYear=newDate(this.getFullYear(),0,1); varmillOfDay=24*60*60*1000; returnMath.ceil((((this-firstDayOfYear)/millOfDay)+firstDayOfYear.getDay()+1)/7); }; /* 01October2013-10:20:20 01November2013-10:20:20 01December2013-10:20:20 08October2013-01:55pm */ varmonthMapping={ "January":"01", "February":"02", "March":"03", "April":"04", "May":"05", "June":"06", "July":"07", "August":"08", "September":"09", "October":"10", "November":"11", "December":"12" }; functionformatAmPm(){ vardateTimeStr=arguments[0]; vardateStr=dateTimeStr.split("-"); if(lang.currentLang=="zh_CN"||lang.currentLang=="zh_TW"){ vars_year=lang.getMessage('yearCn'); vars_month=lang.getMessage('monthCn'); vars_day=lang.getMessage('dayCn'); vardateArray=dateStr[0].split(""); vars_time=dateStr[1].split("")[1]=="am"?lang.getMessage('amCn'):lang.getMessage('pmCn'); varret=dateArray[2]+s_year+monthMapping[dateArray[1]]+s_month+dateArray[0]+s_day+""+s_time+dateStr[1].split("")[0]; if(arguments.length>1){ /*adminfeature,sousingendate*/ ret=arguments[1]?dateStr[0]:dateStr[1]; } returnret; } else{ if(arguments.length>1){ returnarguments[1]?dateStr[0]:dateStr[1]; } else{ returndateStr[0]+""+dateStr[1]; } } } functionformatDate(dateTime,pattern){ if(pattern==""||pattern==null){ pattern="/"; } vardate=newDate(dateTime); varyear=date.getFullYear(); varmonth=date.getMonth()+1; month=month<10?'0'+month:month; varday=date.getDate(); day=day<10?'0'+day:day; returnmonth+pattern+day+pattern+year; } functionformatDateDDMMYYYY(dateTime){ varymd=dateTime.split("")[0]; vardateArray=ymd.split("-"); returndateArray[2]+"."+dateArray[1]+"."+dateArray[0]; } /** * *@paramdateTimeYYYY-MM-DDhh:mm:ss */ functionformatDateDDMMYY(dateTime){ varymd=dateTime.split("")[0]; vardateArray=ymd.split("-"); returndateArray[2]+"."+dateArray[1]+"."+dateArray[0].substring(2); } /**********************calculatedate*****************************/ varMONTH=[ {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} ]; varDAYS=[31,28,31,30,31,30,31,31,30,31,30,31]; functiongetYears(){ varyears=[]; vardate=newDate(); varyear=date.getFullYear(); varminYear=year-10; for(varcurrentYear=minYear;currentYear<=year;currentYear++){ years.push(currentYear); } returnyears; }; functionfillinMonthOptions(id,currentMonth){ $("#"+id).get(0).options.length=0; for(vari=0;i"+MONTH[i].key+""); } else{ $("#"+id).append(" "+MONTH[i].key+""); } } }; functionfillinAllMonthOptions(monthIds,currentMonth){ //varmonthIds=["loginMonth_1","loginMonth_2","auditMonth_1","auditMonth_2"]; for(vari=0;i "+years[i]+""); } else{ $("#"+id).append(" "+years[i]+""); } } }; functionfillinAllYearOptions(yearIds,currentYear){ //varyearIds=["loginYear","auditYear"]; years=getYears(); for(vari=0;i "+i+""); } } functionfiilinDays(year,month,dayIds){ varmonthDays=getMonthDays(year,month); for(vari=0;i "+week[0]+"("+week[1]+"-"+week[2]+")"); } } functionfillinWeeks(year,weekIds){ for(vari=0;i PS:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用:
在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq在线天数计算器:
http://tools.jb51.net/jisuanqi/datejsq更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript时间与日期操作技巧总结》、《JavaScript+HTML5特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。