欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 21:39:34
本文实例讲述了js实现处理时间,年月日,星期的公共方法。分享给大家供大家参考,具体如下: 在项目中用到的,用js 来得到年月日,星期,当前多少个周的函数,备忘在这里,以后...

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

在线日期/天数计算器:

在线日期计算器/相差天数计算器:

在线日期天数差计算器:

在线天数计算器:

更多关于javascript相关内容感兴趣的读者可查看本站专题:《javascript时间与日期操作技巧总结》、《javascript+html5特效与技巧汇总》、《javascript错误与调试技巧总结》、《javascript数据结构与算法技巧总结》及《javascript数学运算用法总结

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

上一篇:

下一篇: