极乐门资源网 Design By www.ioogu.com
本文实例讲述了JS实现处理时间,年月日,星期的公共方法。分享给大家供大家参考,具体如下:
在项目中用到的,用js 来得到年月日,星期,当前多少个周的函数,备忘在这里,以后参考。
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" " " + s_time + dateStr[1].split(" ")[0] ; if ( arguments.length > 1 ){ /*admin feature ,so using en date*/ ret = arguments[1] " " + 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]; 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) "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:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用:
在线日期/天数计算器:
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程序设计有所帮助。
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无JS实现处理时间,年月日,星期的公共方法示例的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年12月28日
2024年12月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]