JS实现处理时间,年月日,星期的公共方法示例


Posted in Javascript onMay 31, 2019

本文实例讲述了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" ? 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:这里再为大家推荐几款比较实用的天数计算在线工具供大家使用:

在线日期/天数计算器:
http://tools.3water.com/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.3water.com/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.3water.com/jisuanqi/onlinedatejsq

在线天数计算器:
http://tools.3water.com/jisuanqi/datejsq

希望本文所述对大家JavaScript程序设计有所帮助。

Javascript 相关文章推荐
jQuery 行背景颜色的交替显示(隔行变色)实现代码
Dec 13 Javascript
深入分析js中的constructor和prototype
Apr 07 Javascript
9行javascript代码获取QQ群成员具体实现
Oct 16 Javascript
JQuery Highcharts 动态生成图表的方法
Nov 15 Javascript
浅谈jQuery的offset()方法及示例分享
Jul 17 Javascript
详解AngularJS中自定义过滤器
Dec 28 Javascript
jquery跟随屏幕滚动效果的实现代码
Apr 13 Javascript
vue组件父与子通信详解(一)
Nov 07 Javascript
基于vue.js实现的分页
Mar 13 Javascript
vue自动路由-单页面项目(非build时构建)
Apr 30 Javascript
node.js事件轮询机制原理知识点
Dec 22 Javascript
vue中控制mock在开发环境使用,在生产环境禁用方式
Apr 06 Vue.js
一文了解vue-router之hash模式和history模式
May 31 #Javascript
vue App.vue中的公共组件改变值触发其他组件或.vue页面监听
May 31 #Javascript
微信小程序环境下将文件上传到OSS的方法步骤
May 31 #Javascript
Vue Router history模式的配置方法及其原理
May 30 #Javascript
vue-cli3+ts+webpack实现多入口多出口功能
May 30 #Javascript
详解Vue项目引入CreateJS的方法(亲测可用)
May 30 #Javascript
了解JavaScript函数中的默认参数
May 30 #Javascript
You might like
PHP的开发框架的现状和展望
2007/03/16 PHP
Php中文件下载功能实现超详细流程分析
2012/06/13 PHP
安装apache2.2.22配置php5.4(具体操作步骤)
2013/06/26 PHP
如何使用php实现评委评分器
2015/07/31 PHP
使用PHPExcel导出Excel表
2018/09/08 PHP
php实现构建排除当前元素的乘积数组方法
2018/10/06 PHP
javascript替换已有元素replaceChild()使用介绍
2014/04/03 Javascript
js图片处理示例代码
2014/05/12 Javascript
Javascript连接Access数据库完整实例
2015/08/03 Javascript
JavaScript+html5 canvas绘制的小人效果
2016/01/27 Javascript
JavaScript事件代理和委托详解
2016/04/08 Javascript
IE和Firefox之间在JavaScript语法上的差异
2016/04/22 Javascript
jquery延迟对象解析
2016/10/26 Javascript
微信小程序-小说阅读小程序实例(demo)
2017/01/12 Javascript
ie下js不执行的几种可能
2017/02/28 Javascript
基于JavaScript实现类名的添加与移除
2017/04/23 Javascript
通过seajs实现JavaScript的模块开发及按模块加载
2019/06/06 Javascript
简单了解TypeScript中如何继承 Error 类
2019/06/21 Javascript
基于JavaScript实现控制下拉列表
2020/05/08 Javascript
[03:04]2018年国际邀请赛典藏宝瓶&莱恩声望物品展示 片尾有彩蛋
2018/06/04 DOTA
Python实现可自定义大小的截屏功能
2018/01/20 Python
Python闭包执行时值的传递方式实例分析
2018/06/04 Python
python操作日志的封装方法(两种方法)
2019/05/23 Python
Python3 实现文件批量重命名示例代码
2019/06/03 Python
如何使用Flask-Migrate拓展数据库表结构
2019/07/24 Python
应用OpenCV和Python进行SIFT算法的实现详解
2019/08/21 Python
使用pyhon绘图比较两个手机屏幕大小(实例代码)
2020/01/03 Python
python单例模式的应用场景实例讲解
2021/02/24 Python
英国领先的大码时装品牌之一:Elvi
2018/08/26 全球购物
ALDO加拿大官网:加拿大女鞋品牌
2018/12/22 全球购物
社区扶贫帮困工作总结
2015/05/20 职场文书
复兴之路展览观后感
2015/06/02 职场文书
简爱电影观后感
2015/06/10 职场文书
CSS3实现列表无限滚动/轮播效果
2021/06/23 HTML / CSS
Java中CyclicBarrier和CountDownLatch的用法与区别
2021/08/23 Java/Android
mysql sock 文件解析及作用讲解
2022/07/15 MySQL