js中经常用到得方法(构造函数)

JS计算24节气的方法

function getjq(yyyy,mm,dd){
mm = mm-1;
var sTermInfo = new Array(0,21208,42467,63836,85337,107014,128867,150921,173149,195551,218072,240693,263343,285989,308563,331033,353350,375494,397447,419210,440795,462224,483532,504758);
var solarTerm = new Array("小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种","夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪","冬至");
var solarTerms = "";
//  此方法是获取该日期是否为某节气
//    var tmp1 = new Date((31556925974.7*(yyyy-1900)+sTermInfo[mm*2+1]*60000)+Date.UTC(1900,0,6,2,5));
//    var tmp2 = tmp1.getUTCDate();
//    if (tmp2==dd)
//        solarTerms = solarTerm[mm*2+1];
//    console.log(solarTerms);
//    tmp1 = new Date((31556925974.7*(yyyy-1900)+sTermInfo[mm*2]*60000)+Date.UTC(1900,0,6,2,5));
//    tmp2= tmp1.getUTCDate();
//    if (tmp2==dd)
//        solarTerms = solarTerm[mm*2];

//  此方法可以获取该日期处于某节气
while (solarTerms==""){ var tmp1 = new Date((31556925974.7*(yyyy-1900)+sTermInfo[mm*2+1]*60000)+Date.UTC(1900,0,6,2,5)); var tmp2 = tmp1.getUTCDate(); if (tmp2==dd) solarTerms = solarTerm[mm*2+1];tmp1 = new Date((31556925974.7*(yyyy-1900)+sTermInfo[mm*2]*60000)+Date.UTC(1900,0,6,2,5)); tmp2= tmp1.getUTCDate(); if (tmp2==dd) solarTerms = solarTerm[mm*2]; if(dd>1){ dd=dd-1; }else { mm=mm-1;   if(mm<0){     yyyy=yyyy-1; mm=11;   }   dd=31;   } } return solarTerms; }

js时间戳转换为时间用newnum来确定返回得种类

//时间戳转换
function ToolsTime (time, newnum) {
  if (!time) {
    return;
  }
  time = time.length == 10 ? time * 1000 : time;
  const date = new Date(time);
  const dateNumFun = (num) => (num < 10 ? `0${num}` : num);
  const [Y, M, D, h, m, s] = [
    // es6 解构赋值
    date.getFullYear(),
    dateNumFun(date.getMonth() + 1),
    dateNumFun(date.getDate()),
    dateNumFun(date.getHours()),
    dateNumFun(date.getMinutes()),
    dateNumFun(date.getSeconds()),
  ];
  if (newnum == 0) {
    return `${Y}-${M}-${D} ${h}:${m}:${s}`; // 一定要注意是反引号,否则无效。
  }
  if (newnum == 1) {
    return `${Y}-${M}-${D}`;
  }
  if (newnum == 2) {
    return `${Y}`;
  }
  if (newnum == 3) {
    return `${M}`;
  }
  if (newnum == 4) {
    return `${D}`;
  }
  // return `${Y}-${M}-${D} ${h}:${m}:${s}`; // 一定要注意是反引号,否则无效。
};

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值