zoukankan      html  css  js  c++  java
  • JS之路——日期函数

    时间对象是一个我们经常要用到的对象,无论是做时间输出、时间判断等操作时都与这个对象离不开。除开JavaScript中的时间对象外,在VbScript中也有许多的时间对象,而且非常好用。下面还是按照我们的流程来进行讲解。

    它是一个内置对象——而不是其它对象的属性,允许用户执行各种使用日期和时间的过程。
     方法:分为得到时间方法、设置时间方法和转换时间方法

    得到时间方法:
      getDate() 查看Date对象并返回日期
      getDay() 返回星期几
      getHours() 返回小时数
      getMinutes() 返回分钟数
      getMonth() 返回月份值
      getSeconds() 返回秒数
      getTime() 返回完整的时间
      getYear() 返回年份
      

    设置时间方法:
      setDate() 改变Date对象的日期
      setHours() 改变小时数
      setMinutes() 改变分钟数
      setMonth() 改变月份
      setSeconds() 改变秒数
      setTime() 改变完整的时间
      setYear() 改变年份

    转换时间方法:
      toGMTString() 把Date对象的日期(一个数值)转变成一个GMT时间字符串,返回类似下面的值:Weds,15 June l997 14:02:02 GMT(精确的格式依赖于计算机上所运行的操作系统而变)
      toLocaleString() 把Date对象的日期(一个数值)转变成一个字符串,使用所在计算机上配置使用的特定日期格式
      UTC() 使用Date UTC(年、月、日、时、分、秒),以自从1970年1月1日00:00:00(其中时、分、秒是可选的)以来的毫秒数的形式返回日期
    几个需要注意的地方:

    1、得到日期和年和设置日期和年时间,其中很怪的问题就是不能对月份进行设置(比较的怪):
    <script language="javascript">
    d = new Date();
    alert(d.toLocaleString());
    d.setDate(25);
    alert(d.toLocaleString());
    d.setYear(2000);
    alert(d.toLocaleString());
    </script>

    2、获得年的时候最好用getFullYear()方法来做

    3、由于针对月份,JS是从0开始的,因此需要对月份进行操作时要加1



    下面是几个关于时间的经典而且经常会用到的例子,希望对大家会有提高的。谢谢继续关注该帖子。。。

    1、将2005-8-5转换成2005-08-05格式
    <script language="javascript">
    var strDate = '2005-8-5';
    window.alert(strDate.replace(/(w)/g, '0$1'));
    </script>

    2、得到间隔天数
    <script type="text/javascript">
    <!--
    alert("间隔天数为:"+(new Date('2005/8/15')-new Date('2003/9/18'))/1000/60/60/24+"天")
    //-->
    </script>

    3、得到间隔时间
    <script>
    var d1=new Date("2004/09/16 20:08:00");
    var d2=new Date("2004/09/16 10:18:03");
    var d3=d1-d2;
    var h=Math.floor(d3/3600000);
    var m=Math.floor((d3-h*3600000)/60000);
    var s=(d3-h*3600000-m*60000)/1000;
    alert("相差"+h+"小时"+m+"分"+s+"秒");
    </script>

    4、得到今天的日期
    <script language="javascript">
    d = new Date();
    alert(d.getFullYear()+"年"+(d.getMonth()+1)+"月"+d.getDate()+"日");
    </script>

    6、数字日期转汉字
    <html>
    <head>
    <title> New Document </title>
    </head>

    <body>

    <script language=javascript>
    Date.prototype.getRead = function()
    {
    var values = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九");
    var returnValue, temp;
    returnValue = this.getYear()+"年";
    temp = (this.getMonth()+1)+"月"+this.getDate()+"日";
    temp = temp.replace(/(d)(d)/g,"$1十$2").replace(/1十/g,"十").replace(/十0/g,"十");
    returnValue += temp;
    returnValue = returnValue.replace(/d/g, function(sts){return values[parseInt(sts)]});
    return returnValue;
    }

    var t=new Date();
    document.write(t.getRead());
    </script>
    </body>
    </html>

    7、得到前N天或后N天的日期
    方法一:
    <script type="text/javascript">
    function showdate(n)
    {
    var uom = new Date(new Date()-0+n*86400000);
    uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
    return uom;
    }

    window.alert("今天是:"+showdate(0));
    window.alert("昨天是:"+showdate(-1));
    window.alert("明天是:"+showdate(1));
    window.alert("10天前是:"+showdate(-10));
    window.alert("5天后是:"+showdate(5));
    </script>
    方法二:
    <script type="text/javascript">
    function showdate(n)
    {
    var uom = new Date();
    uom.setDate(uom.getDate()+n);
    uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate();
    return uom;
    }

    window.alert("今天是:"+showdate(0));
    window.alert("昨天是:"+showdate(-1));
    window.alert("明天是:"+showdate(1));
    window.alert("10天前是:"+showdate(-10));
    window.alert("5天后是:"+showdate(5));
    </script>
    方法三(不好意思,这个市用vsscript做的):
    <script language="vbscript">
    function showdate(n)
    showdate=dateadd("d",date(),n)
    end function
    msgbox "今天是:"&showdate(0)
    msgbox "昨天是:"&showdate(-1)
    msgbox "明天是:"&showdate(1)
    msgbox "十天前是:"&showdate(-10)
    msgbox "五天后是:"&showdate(5)
    </script>
    方法四:
    <script language="Javascript">
    Date.prototype.getDays=function(){
    var _newDate=new Date();
    _newDate.setMonth(_newDate.getMonth()+1);
    _newDate.setDate(0);
    $_days=_newDate.getDate();
    delete _newDate;
    return $_days;
    }
    function showdate(n)
    {
    var uom = new Date();
    uom.setDate(uom.getDate()+n);
    uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate()+" 星期"+('天一二三四五六'.charAt(uom.getDay()))+" 本月有"+ uom.getDays()+"天";
    return uom;
    }

    window.alert("今天是:"+showdate(0));
    window.alert("昨天是:"+showdate(-1));
    window.alert("明天是:"+showdate(1));
    window.alert("10天前是:"+showdate(-10));
    window.alert("5天后是:"+showdate(5));
    </script>

  • 相关阅读:
    27. Remove Element
    列表变成字典
    1. Two Sum
    CVPR2019:What and How Well You Performed? A Multitask Learning Approach to Action Quality Assessment
    959. Regions Cut By Slashes
    118. Pascal's Triangle
    loj3117 IOI2017 接线 wiring 题解
    题解 NOI2019 序列
    题解 省选联考2020 组合数问题
    题解 Educational Codeforces Round 90 (Rated for Div. 2) (CF1373)
  • 原文地址:https://www.cnblogs.com/xj76149095/p/5699814.html
Copyright © 2011-2022 走看看