Date转为字符串

<p id = "demo"></p>
<script type="text/javascript">
        var box = new Date();
                   document.write(box.toUTCString());
         alert(box.toString());
 
          var myVar = setInterval(myTimer,1000);
 
          function myTimer() {
                 var d = new Date();
                 document.getElementById("demo").innerHTML = d.toLocaleTimeString();
          }

 

把日期转为字符串:

          toString()                                       把Date 对象转换为字符串。默认的

          toTimeString()                               把Date 对象的时间部分转换为字符串

          toDateString()                               把Date 对象的日期部分转换为字符串

          toUTCString()                                根据世界时间,把Date对象转换为字符串

          toLocaleString()                             根据本地时间格式,把Date对象转换为字符串,本地的

          toLocaleTimeString()                     根据本地时间格式,把Date对象的时间部分转换为字符串。

          toLocaleDateString()                      根据本地时间格式,把Date对象的日期部分转换为字符串。

***********************************************************************************

Date输出常用的日期格式

<style>
       #mytime {
              background:#000;
              color:#fff;
              text-align:center;
}
</style>
<body>
     <h1 id="mytime">显示时间</h1> 
     <script>
              //alert(habit);
              setInterval(function(){     //setInterval()方法可按照指定的周期(以毫秒计)来调用函数或计算表达式
                      var habit = new Date();
                      year = habit.getFullYear();
                      month = habit.getMonth() + 1;
                      month = checkTime(month);
                      day = habit.getDate();
                      week = habit.getDay();
                      switch(week) {
                             case 1:
                                    week = "星期一";
                                    break;
                             case 2:
                                    week = "星期二";
                                    break;
           case 3:
                                      week = "星期三";
                                      break;
                               case 4:
                                      week = "星期四";
                                      break;
                               case 5:
                                      week = "星期五";
                                      break;
                               case 6:
                                      week = "星期六";
                                      break;
                               default:
                                      week = "星期日";
                                      break;
}
hour = habit.getHours(),
minute = habit.getMinutes();
minute = checkTime(minete);
second = habit.getSeconds();
second = checkTime(second);
time = (year + '-' + month + '-' + day + ' ' + week + ' ' + hour + ':' + minute + ':' + second);
var obj = document.getElementById('mytime');
obj.innerHTML = time;
//document.title = time;
},1000)
 
function checkTime(i){ //封装一个数字在小于10时在前面加零
       if(i<10){
          i = "0" + i
       }//在数字<10之前加零
       return i;
}
     </script>
</body>

*********************************************************************************

Date获得和设置

<script type = "text/javascript">
        var box = new Date(2019,7,19,13,19,45,30);
                         alert(box);               //Mon Aug 19 2019 13:19:45   GMT+0800(中国标准时间)
                         alert('toString:' + box.toLocaleString());   //toLocaleString:2019/8/19下午1:19:45
                         alert('valueOf:' + box.valueOf());   //valueof:1566191985030
        var box = new Date();
        document.write(box);
        //box.setMonth(11);
  
        //alert(box.getMonth()+1);
 
        //toLocaleTimeString()           根据本地时间输出本地时间   下午1:19:45
        //box.toLocaleDateString()        根据本地年月日输出本地年月日  结果:2019/8/19
</script>

***********************************************************************************

JS里面获取和设置日期的方法:

Date   对象用于处理日期和时间 返回当前的日期和时间。

             Date   对象会自动把当前日期和时间保存为其初始值。

获取毫秒数:

1、Date.parse()是Date对象的静态方法  获得的是1970年1月1日零时到现在的毫秒数

                      2、alert('valueOf:' + box.valueOf());      返回Date对象的原始值以毫秒数

                      3、Date。UTC()     根据世界时返回1970年1月1日到指定日期的毫秒数。

                      4、getTime()   返回1970年1月1日至今的毫秒数。

                      5、Number(new Date("2019-04-15"));   日期转换为数字也是1970年1月1日至今的毫秒数。

获取时间:

                 1、var box = new Date(1566191985030);

                      2、var box = new Date('2019/8/19');

                      3、var box = new Date(2019,7,19,13,19,45,30);

获取方法用于获取日期的某个部分  下面是最常用的方法:

                 getDate()                以数值返回天(1-31)

                      getDay()                 以数值获取周名(0-6)   //注意0星期日

                      getFullYear()           获取四位的年(yyyy)

                      getHours()              获取小时(0-23)

                      getMilliseconds()    获取毫秒(0-999)

                      getMinutes()           获取分(0-59)

                      getMonth()+1         获取月(0-11)   要加1才是最终的月份数

                      getSeconds()           获取秒(0-59)

                      getTime()                 获取时间(从1970年1月1日至今)

 

                      getUTCDate()           等于getDate(),但返回UTC 日期

                      getUTCDay()            等于getDay(),但返回UTC 日

                      getUTCFullYear()      等于getFullYear(),但返回UTC年

                      getUTCHours()         等于getHours(),但返回UTC 小时

                      getUTCMilliseconds()        等于getMilliseconds(),但返回UTC毫秒

                      getUTCMInutes()      等于getMInutes(),但返回UTC分

                      getUTCMonth()        等于getMonth(),但返回UTC月

                      getUTCSeconds()     等于getSeconds(),但返回UTC秒

**********************************************************************************

getHours()和getUTCHours()相差8小时         东八区由八个小时的时差。

 

getTimezoneOffset()   返回本地时间与格林威治标准时间(GMT)   的分钟差。

**********************************************************************************

设置方法用于设置日期的某个部分 下面是最常用的方法:

var box = new Date();
                  box.setMonth(11);
                  alert(box);
                  
                  setDate()                                  设置Date 对象中月的某一天(1~31)。
                  setMonth()                               设置Date 对象中月份(0~11).
                  setFullYear()                             设置Date 对象中的年份(四位数字)
                  setHours()                                设置Date 对象中的小时(0~23)。
                  setMinutes()                             设置Date 对象中的分钟(0~59)。
                  setSeconds()                            设置Date 对象中的秒钟(0~59)。
                  setMilliseconds()                      设置Date 对象中的毫秒(0~999)。
                  setTime()                                  以毫秒设置Date 对象
 
                  setUTCDate()                 根据世界时设置Date 对象中月份的一天(1~31)
                  setUTCMonth()              根据世界时设置Date 对象中的月份(0~11)
                  setUTCFullYear()            根据世界时设置Date 对象中的年份(四位数字)
                  setUTCHours()               根据世界时设置Date 对象中的小时(0~23)
                  setUTCMinutes()            根据世界时设置Date 对象中的分钟(0~59)
                  setUTCSeconds()           根据世界时设置Date 对象中的秒钟(0~59)
                  setUTCMilliseconds()     根据世界时设置Date 对象中的毫秒(0~999)
                  var today,someday,text;
                        today = new Date();
                        someday = new Date();
                        someday.setFullTear(2049,0,16);
              
                        if (someday > today) {
                            text = "今天在2049年1月16日之前";
                        } else {
                           text = "今天在2049年1月16日之后";                           
                        }
                        alert(text);