zoukankan      html  css  js  c++  java
  • js(javascript)自定义的DateAdd和DateDiff函数以及当前日期的取法

    js对日期的处理比较垃圾,下面的几个函数可能帮助你更好的处理js中的日期.
    包括,dateadd,datediff以及当前日期

    当前日期
    var d = new date();
    var theday = d.getYear() + "-" +((d.getMonth().toString().length>1) ? (d.getMonth() + 1) : "0"+(d.getMonth() + 1)) + "-" + ((d.getDate().toString().length>1) ? d.getDate() : "0"+d.getDate()) ;


    dateadd函数
        function DateAdd(interval, num, dateValue)
        {
            var newCom = new TimeCom(dateValue);
            switch(String(interval).toLowerCase())
            {
                case "y": case "year": newCom.year += num; break;
                case "m": case "month": newCom.month += num; break;
                case "d": case "day": newCom.day += num; break;
                case "h": case "hour": newCom.hour += num; break;
                case "min": case "minute": newCom.minute += num; break;
                case "s": case "second": newCom.second += num; break;
                case "ms": case "msecond": newCom.msecond += num; break;
                case "w": case "week": newCom.day += num*7; break;
                default: return("invalid");
            }
            var now = newCom.year+"/"+newCom.month+"/"+newCom.day+" "+newCom.hour+":"+newCom.minute+":"+newCom.second;
            return(new Date(now));
        }
        
    TimeCom对象
        function TimeCom( dateValue )
        {
      var newCom;
      if (dateValue=="")
      {
       newCom = new Date();
      }else{
       newCom = new Date(dateValue);
      }
            this.year = newCom.getYear();
            this.month = newCom.getMonth()+1;
            this.day = newCom.getDate();
            this.hour = newCom.getHours();
            this.minute = newCom.getMinutes();
            this.second = newCom.getSeconds();
            this.msecond = newCom.getMilliseconds();
            this.week = newCom.getDay();
        }



    datediff函数

        function DateDiff(interval,date1,date2)
        {
            var TimeCom1 = new TimeCom(date1);
            var TimeCom2 = new TimeCom(date2);
            var result;
            switch(String(interval).toLowerCase())
            {
                case "y":
                case "year":
                result = TimeCom1.year-TimeCom2.year;
                break;
                case "m":
                case "month":
                result = (TimeCom1.year-TimeCom2.year)*12+(TimeCom1.month-TimeCom2.month);
                break;
                case "d":
                case "day":
                result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24));
                break;
                case "h":
                case "hour":
                result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour))/(1000*60*60));
                break;
                case "min":
                case "minute":
                result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute))/(1000*60));
                break;
                case "s":
                case "second":
                result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second))/1000);
                break;
                case "ms":
                case "msecond":
                result = Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day,TimeCom1.hour,TimeCom1.minute,TimeCom1.second,TimeCom1.msecond)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day,TimeCom2.hour,TimeCom2.minute,TimeCom2.second,TimeCom1.msecond);
                break;
                case "w":
                case "week":
                result = Math.round((Date.UTC(TimeCom1.year,TimeCom1.month-1,TimeCom1.day)-Date.UTC(TimeCom2.year,TimeCom2.month-1,TimeCom2.day))/(1000*60*60*24)) % 7;
                break;
                default:
                result = "invalid";
            }
            return(result);
        }

  • 相关阅读:
    JS和PYTHON中数据类型比较
    http状态码
    ffmpeg architecture(上)
    降低数值精度以提高深度学习性能
    IaaS、PaaS 和 SaaS:云服务模型概述
    英特尔Intel® Arria® 10 FPGA加速器设计
    基于至强® 平台的内存数据库解决方案
    MLPerf结果证实至强® 可有效助力深度学习训练
    如何从数据角度为人工智能部署做好数据准备
    英特尔内存革新助平安云 Redis 云服务降本增效
  • 原文地址:https://www.cnblogs.com/rxie/p/1761619.html
Copyright © 2011-2022 走看看